Provide Best Programming Tutorials

Java – Convert String to int

Convert String to int

You can use Integer.parseInt() to convert a String to int.

  //convert String to int
  String a = "123";
  int a1 = Integer.parseInt(a);
  System.out.println(a1);

Output

123

Convert int to String

   //convert int to String
   int b = 123;
   String b1 = String.valueOf(b);
   System.out.println(b1);
123

Note
In summary, parseInt(String) returns a primitive int, whereas valueOf(String) returns a new Integer() object.

Leave a Reply

Close Menu