CONVERTING STRING TO INT IN JAVA?

Manoj Bhatt

Total Post:154

Points:1086
Posted by  Manoj Bhatt
Java  String 
 853  View(s)
Ratings:
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.
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: Converting String to int in Java?

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!