In Java, the conversion of a String variable into an int variable is considered a common process or operation. It may occur when the user's input is processed or manipulated. We can convert a String to an int in Java using the Integer.parseInt() and Integer.valueOf() methods. These methods return instances of the Integer class.

It is generally used if we have to perform mathematical operations on a string that contains a number. Whenever we receive data from a TextField or TextArea, the entered data is received as a string. If the entered data is in number format, we need to convert the string to an int. To do so, we use the Integer.parseInt() method.
The method parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value.
The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method. It throws a NumberFormatException if the string does not contain a parsable integer.
Syntax:
Output:
Parsed integer: 2025
Explanation
The above program takes the string "2025" and converts it into the integer 2025 using Integer.parseInt(). It's especially useful when we are reading numbers from user input or files where values are stored as strings.
The method parses the string as a signed integer for the first argument and an integer in the specified radix for the second argument. The default radix value used in Java is 10, if not specified. The characters in the string must all be digits of the specified radix, except for the first character. The first character may be a + or - sign that represents a positive or negative value. The method returns an integer value as a result.
It throws a NumberFormatException if any of the following situation occurs:
Syntax:
public static int parseInt(String s, int radix) throws NumberFormatException
| Method | Returns |
|---|---|
| parseInt("0", 10) | 0 |
| parseInt("473", 10) | 473 |
| parseInt("+42", 10) | 42 |
| parseInt("-0", 10) | 0 |
| parseInt("-FF", 16) | -255 |
| parseInt("1100110", 2) | 102 |
| parseInt("2147483647", 10) | 2147483647 |
| parseInt("-2147483648", 10) | -2147483648 |
| parseInt("2147483648", 10) | throws a NumberFormatException |
| parseInt("99", 8) | throws a NumberFormatException |
| parseInt("PERL", 10) | throws a NumberFormatException |
| parseInt("Kona", 27) | 411787 |
Output:
x = 5 y = 768 z = -1356 a = 0 b = 225 c = 63 p = 0 q = -253 r = 204 t = 2147483647 u = -2147483648 m = 411787
In the above program, note that the variables v, k, and l are comments because they throw NumberFormatException. You can check by removing the comments.
The Integer.valueOf() method in Java is a static method used to convert a value-either a primitive int or a String-into an Integer object. It is a part of the java.lang.Integer class is often used when we need an Integer object rather than a primitive int. The method has the following three overloaded variants:
Output:
200
If we do not have numbers in a string literal, call Integer.parseInt() or Integer.valueOf() methods throw NumberFormatException.
Output:
Exception in thread "main" java.lang.NumberFormatException: For input string: "hello" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:662) at java.base/java.lang.Integer.parseInt(Integer.java:778) at Main.main(Main.java:5)
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India