--- title: Java Convert String to int - Scaler Topics description: Learn how to convert string to int in Java using two methods, namely integer.parseInt(), integer.valueOf() methods, along with examples on scaler Topics. author: Ayush Agarwal category: Java --- :::section{.abstract} `Integer.parseInt()` and `Integer.valueOf()` methods can be used to convert `String` to `integer` in Java. These methods are defined under the `Integer` class in **java.lang** package. The `Integer.parseInt()` method converts `String` to `int` (primitive data type) in Java. However, the Integer.valueOf() method can be used to convert `String` to an instance of the `Integer` class in Java. Both methods throw `NumberFormatException` when the String input contains characters other than digits. ![Convert String to Integer in Java](https://scaler.com/topics/images/string-to-int-in-java-1.gif) ::: :::section{.main} ## Convert String to int in Java: Integer.parseInt() The [parseInt()](https://www.scaler.com/topics/parseint-in-java/) method converts a `String` to a primitive (basic) data type, i.e., `int` in Java. The `parseInt()` method is a static Integer class method. ### Signature ```java public static int parseInt(String input) ``` ### Syntax ```java int val = Integer.parseInt("200"); ``` ### Example ```java //converting string to integer in java class Example { public static void main(String args[]) { // String literal String str = "123"; // Holds the integer equivalent int ans = Integer.parseInt(str); System.out.println(ans); } } ``` **Output:** ```plaintext 123 ``` ![Convert string to int in java](https://scaler.com/topics/images/string-to-int-in-java-2.gif) ::: ## Convert String to int in Java: Integer.valueOf() `Integer.valueOf()` method returns the `Integer` object when a string literal is passed as an argument. It takes in the string or an integer as a first argument and converts it into an `Integer` object. This works the same as `parseInt()` and changes a string to an integer in java for the radix argument. ### Syntax: ```java Integer.valueOf(String s) Integer.valueOf(String s, int radix) // radix = base of String s ``` ### Example: ```java //converting string to integer in java class Example { public static void main(String args[]) { // String literal String str = "789"; // Holds the integer equivalent assuming base 10 interpretation of str Integer ans = Integer.valueOf(str); // Holds the integer equivalent assuming base 16 interpretation of str Integer ans16 = Integer.valueOf(str, 16); System.out.println(ans); System.out.println(ans16); } } ``` **Output:** ```plaintext 789 1929 ``` ![Convert string to integer java](https://scaler.com/topics/images/string-to-int-in-java-3.gif) ::: :::section{.main} ## NumberFormatException Case While converting string to integer in java using the above method, If the string literal provided as an argument is ill-formatted, i.e., contains characters other than numerics, `NumberFormatException` is thrown. The `Integer.parseInt()` and `Integer.valueOf()` throw NumberFormatException when the string input contains invalid characters. ### Code: ```java class NumberFormatExceptionExample { public static void main(String args[]) { // Contains characters that cannot be converted to integers String str = "abc123"; // throws NumberFormatException int ans = Integer.parseInt(str); System.out.println(ans + "\n"); } } ``` **Output:** ```plaintext Exception in thread "main" java.lang.NumberFormatException: For input string: "abc123" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Integer.parseInt(Integer.java:652) at java.base/java.lang.Integer.parseInt(Integer.java:770) at Main.main(Main.java:8) ``` ### Important Points * The presence of leading zeroes in the string will not be counted using the `parseInt()` or the `valueOf()` method. * The first character, ‘+’ or ‘-’, indicates the sign of the number, which is accepted by both methods of changing the String to int in Java. **Code:** ```java class Example { public static void main(String args[]) { // String with leading zeroes String leadingZero = "00789"; // Strings with a sign String signConvert = "-12"; int ans = Integer.parseInt(leadingZero); int ans1 = Integer.parseInt(signConvert); System.out.println(ans); // outputs 789 System.out.println(ans1); // outputs -12 } } ``` **Output:** ```plaintext 789 12 ``` ::: :::section{.summary} ## Conclusion * `Integer.parseInt()` converts a String to an int primitive data type, while `Integer.valueOf()` converts it to an Integer object. * Both methods throw a `NumberFormatException` if the input String is null, empty, or contains non-numeric characters. * Overloaded versions of both methods allow specifying the radix (base) of the String representation for conversion, which is useful for parsing strings representing numbers in non-decimal bases like binary or hexadecimal. :::