Conversion from String to Integer in Java

Java conversion from String to Integer: Here, we are going to learn how to convert a given string value to an integer in Java?
Submitted by IncludeHelp, on July 16, 2019

Given a string value and we have to convert it into an integer.

Java conversion from String to Integer

To convert a String to Integer, we can use the following methods of Integer class (see the syntax given below...)

Syntax:

    Integer Integer.valueOf(String).intValue();
    OR
    Integer Integer.parseInt(String);

Java code to convert a String to Integer

//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

Java Conversion Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.