Convert String to Int in Java with examples

Integer.parseInt()

We can convert the String value to int in Java using Integer.parseInt() method. If the String variable contains only numbers and you want to return a value as int, this Integer.parseInt() will help you on this.

Convert String to Int (String variable contains only numbers)

Output

Integer.valueOf() method:

The Integer.valueOf() method helps to convert the String to Integer Object. If you want to convert the Integer Object to int value,you can use intValue() method on the Integer Object.

Output:

NumberFormatException

If the String variable contains both String and numbers, the Integer.parseInt() will throw an Number format exception error as below.

Output: