Conversion From String to Integer in Java

You are currently viewing Conversion From String to Integer in Java

Conversion from String to Integer in Java

//Java code to convert String to Integer
public class Main {
    public static void main(String args[]) {
        String str = "12345";

        //variable to store result
        int result = 0;

        //converting string to integer
        //method 1
        result = Integer.valueOf(str).intValue();
        System.out.println("result (value of str as integer) = " + result);

        //method 2
        result = Integer.parseInt(str);
        System.out.println("result (value of str as integer) = " + result);
    }
}

Output

result (value of str as integer) = 12345
result (value of str as integer) = 12345

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply