logo image

How to convert String into int in Java?



There are two ways you can convert a String into integer in Java

  1. Using Integer.parseInt(String input) - This returns primitive int
String input = "123";
int output = Integer.parseInt(input);
System.out.println(output);
  1. Using Integer.valueOf(String input) - This returns cached instance of Integer type
String input = "123";
Integer output = Integer.valueOf(input);
System.out.println(output);

Note: Code will through NumberFormatException if you do not pass a valid number String. Ex. if you pass 123.23 that causes NumberFormatException.