Java String to Int

This article shows how to convert a Java string to int .

To convert a Java String that represents an integer value, to the int type, one can use the utility methods of the Integer class.

Convert Java string to int using Integer.parseInt()

Output

 

Alternatively, you can also use the Integer.valueOf() to achieve the same.

Convert Java string to int using Integer.valueOf()

Output

 

However, if the string does not represent a integer value then any attempt to convert such a string to int would throw java.lang.NumberFormatException.

Converting a non-integer string

Output

 


 

Leave a Reply

avatar
300
  Subscribe  
Notify of