Convert String to int in Java

Tags: String int Integer Convert

In this Java core tutorial, we learn how to convert String value into int value in Java via different solutions.

Parse a String to int value using Integer.parseInt() static method in Java

ConvertStringToIntExample1.java

public class ConvertStringToIntExample1 {
    public static void main(String[] args) {
        String value1 = "123";
        int value2 = Integer.parseInt(value1);

        System.out.println("String value: " + value1);
        System.out.println("int value: " + value2);
    }
}
The output is:
String value: 123
int value: 123

Using Integer.valueOf() to instantiate an Integer from a String value

ConvertStringToIntExample2.java

public class ConvertStringToIntExample2 {
    public static void main(String[] args) {
        String value1 = "456";
        int value2 = Integer.valueOf(value1);

        System.out.println("String value: " + value1);
        System.out.println("int value: " + value2);
    }
}
The output is:
String value: 456
int value: 456

Happy Coding 😊