Java convert String to Int

In Java, you can use Integer.parseInt() to convert a String to int.

1. Integer.parseInt() Examples

Example to convert a String “10” to an primitive int.

String number = "10";
int result = Integer.parseInt(number);
System.out.println(result);

Output

10

2. Integer.valueOf() Examples

Alternatively, you can use Integer.valueOf(), it will returns an Integer object.

String number = "10";
Integer result = Integer.valueOf(number);
System.out.println(result);

Output

10
Note
In summary, parseInt(String) returns a primitive int, whereas valueOf(String) returns a new Integer() object.
Fuente: https://www.mkyong.com/java/java-convert-string-to-int/
Advertisements
This entry was posted in Compus, General and tagged . Bookmark the permalink.