How to convert String into int in Java?

Views: 162   

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);
  2. Using Integer.valueOf(String input) - This returns cached instance of Integer type
    String input = "123";
    Integer output = Integer.valueOf(input);
    Note: Code will through NumberFormatException if you do not pass a valid number String.
    Ex. if you pass 123.23 that causes NumberFormatException.
On By