Home > DeveloperSection > Forums > Converting String to int in Java?
Manoj Bhatt
Manoj Bhatt

Total Post:154

Posted on    May-07-2015 1:25 AM

 Java Java  String 

 1 Reply(s)
 825  View(s)
Rate this:
How can a String be converted to an int in Java?

My String contains only numbers and I want to return the number it represents.

For example, given the string "1234" the result should be the number 1234.

Mayank Tripathi
Mayank Tripathi

Total Post:397

Posted on    May-07-2015 7:14 AM

Integer x = Integer.valueOf(str);
// or
int y = Integer.parseInt(str);
There is a slight difference between these methods:

valueOf returns new instance of java.lang.Integer
parseInt returns primitive int.
The same is for all cases: Short.valueOf/parseShort, Long.valueOf/parseLong, etc.

Don't want to miss updates? Please click the below button!

Follow MindStick