on Leave a Comment

Java Program to Convert String to Int

In java, we can convert string to integer using static methods like parseInt() and valueOf(). 

Java String to Int Using Integer.parseInt()

parseInt() is a static method of Integer class, this method returns a primitive int.

class StringToInt{
 public static void main(String[] args){
  String str="100";
  
  //String to Int using parseInt method
  int a = Integer.parseInt(str);
  System.out.println("Sum is "+(a+10));
 }
}

Output:

Sum is 110

Java String to Int Using Integer.valueOf()

valueOf() method returns a Integer object.

class StringToInt{
 public static void main(String[] args){
  String str="100";
  
  //String to Int using parseInt method
  Integer a = Integer.valueOf(str);
  System.out.println("Sum is "+(a+10));
 }
}

Output:

Sum is 110




0 comments:

Post a Comment