Wrapper Class - Converting numeric string object to primitive numbers in Java


The program demonstrates the use of the parseInt() and parseFloat() methods of the Integer and Float classes respectively to convert a string representation of a number into a primitive number. In the program, a string "25" is parsed into an int using the parseInt() method and printed to the console using System.out.println(). Similarly, a string "25.25f" is parsed into a float using the parseFloat() method and printed to the console. Note that the parseInt() and parseFloat() methods are static methods of their respective wrapper classes (Integer and Float) and can be used directly without creating an object of the class.

Source Code

public class wrapperClass5 {
    public static void main(String args[])
    {
        //Converting numeric string object to primitive numbers.
        /*String SI="25";
        Integer I= Integer.valueOf(SI);
        int i= I.intValue();
        System.out.println(i);*/
 
        String SI="25";
        int i=Integer.parseInt(SI);
        System.out.println("int Value : "+i);
        String SF="25.25f";
        float f=Float.parseFloat(SF);
        System.out.println("float Value : "+f);
    }
}
 

Output

int Value : 25
float Value : 25.25
To download raw file Click Here

Basic Programs