Wrapper Class - Converting primitive number to number object in Java


A Wrapper class is a class whose object wraps or contains primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store primitive data types. In other words, we can wrap a primitive value into a wrapper class object.

The program demonstrates how to convert primitive data types to their corresponding wrapper class objects in Java. The program starts by defining a class named wrapperClass1. It contains a single method main which is the entry point of the program. Inside the main method, there are several examples of how to convert primitive data types to their wrapper class objects. For each data type, two examples are given.

For example, to convert a byte primitive data type to a Byte object, the program first declares a variable b of type byte and assigns it the value 10. It then declares a Byte object named bo1 and initializes it with new Byte(b). This creates a new Byte object with a value equal to the byte value 10. The program then declares another Byte object named bo2 and initializes it with Byte.valueOf(s1). Here, the valueOf method of the Byte class is used to create a Byte object from a String object s1 containing the value "25".

Similar examples are given for converting short, int, long, float, and double primitive data types to their respective wrapper class objects. Finally, the program prints the values of all the created wrapper class objects using System.out.println statements.

Source Code

public class wrapperClass1 {
    public static void main(String args[])
    {
        //Converting primitive number to number object
        //Byte Number to Byte Object
        byte b=10;
        String s1="25";
        Byte bo1 =new Byte(b);
        Byte bo2 = Byte.valueOf(s1);
        System.out.println("Byte Object-1 : "+bo1);
        System.out.println("Byte Object-2 : "+bo2);
 
        //Short Number to Short Object
        short s=85;
        String s2="95";
        Short so1 =new Short(s);
        Short so2 = Short.valueOf(s2);
        System.out.println("Short Object-1 : "+so1);
        System.out.println("Short Object-2 : "+so2);
 
        //Int Number to Integer Object
        int i=125;
        String s3="100";
        Integer io1 =new Integer(i);
        Integer io2 = Integer.valueOf(s3);
        System.out.println("Integer Object-1 : "+io1);
        System.out.println("Integer Object-2 : "+io2);
 
        //Long Number to Long Object
        long l=122525;
        String s4="100885";
        Long lo1 =new Long(l);
        Long lo2 = Long.valueOf(s4);
        System.out.println("Long Object-1 : "+lo1);
        System.out.println("Long Object-2 : "+lo2);
 
        //Float Number to Float Object
        float f=25.5f;
        String s5="23.25f";
        Float fo1 =new Float(f);
        Float fo2 = Float.valueOf(s5);
        System.out.println("Float Object-1 : "+fo1);
        System.out.println("Float Object-2 : "+fo2);
 
 
        //Double Number to Double Object
        double d=258.5555;
        String s6="223.25";
        Double do1 =new Double(d);
        Double do2 = Double.valueOf(s6);
        System.out.println("Double Object-1 : "+do1);
        System.out.println("Double Object-2 : "+do2);
 
 
    }
}
 

Output

Byte Object-1 : 10
Byte Object-2 : 25
Short Object-1 : 85
Short Object-2 : 95
Integer Object-1 : 125
Integer Object-2 : 100
Long Object-1 : 122525
Long Object-2 : 100885
Float Object-1 : 25.5
Float Object-2 : 23.25
Double Object-1 : 258.5555
Double Object-2 : 223.25
To download raw file Click Here

Basic Programs