Wrapper Class - Converting string object to primitive numbers in Java


This program demonstrates how to convert a String object to primitive number data types using wrapper classes in Java. In the main method, first, a String SI is initialized with value "25". Then, Integer.valueOf() method is used to convert the SI String object to an Integer wrapper object I. Finally, I.intValue() method is used to extract the integer value from the I wrapper object and store it in a primitive integer variable i. The value of i is then printed on the console.

Next, another String SD is initialized with value "25.25". Then, Double.valueOf() method is used to convert the SD String object to a Double wrapper object D. Finally, D.doubleValue() method is used to extract the double value from the D wrapper object and store it in a primitive double variable d. The value of d is then printed on the console. Overall, this program demonstrates how to use wrapper classes to convert String objects to primitive number data types in Java.

Source Code

public class wrapperClass4 {
    public static void main(String args[])
    {
        //Converting string object to primitive numbers.
        String SI="25";
        Integer I= Integer.valueOf(SI);
        int i= I.intValue();
        System.out.println(i);
        String SD="25.25";
        Double D= Double.valueOf(SD);
        double d= D.doubleValue();
        System.out.println(d);
    }
}
 

Output

25
25.25
To download raw file Click Here

Basic Programs