Wrapper Classes in Java

Hello readers, in today’s topic we will discuss wrapper classes in Java. Previously we also discussed various important topics of Java like Data Types in Java, Operator in Java, Keywords in Java, and many more. So without wasting any time let’s dive into today’s topic:

Wrapper Classes in JAVA

Generally, the wrapper class in Java is used to wrap or represent the value of primitive data types (int, boolean, etc.) as an object. It is a type of class whose object contains primitive data types. Here also include some methods which are used to unwrap the object back into a primitive data type. The wrapper class in java provide under java.lang package.

Primitive Data TypeWrapper class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

There are two types of features are available to convert primitives into objects and objects into primitives i.e.: Autoboxing & Unboxing.

Autoboxing

Autoboxing is the process of automatic conversion of primitive data types into the object (to their corresponding wrapper class). Example:-convert int into Integer, byte into Byte, char into Character,  float into Float, double into Double, etc.

Here the use of the valueOf() method is not required to convert the primitive into objects.

Example:-

// Example of a java program for using wrapper class to converting primitive type into Object
public class GeektoCode {
        public static void main(String args[]){
                    int num = 100;
                    //converting of int to Integer
                    Integer object = num; //here compiler can convert this primitive to object internally
                    System.out.println(object);
        }
}

OUTPUT:-

100

In the above example normally a value is assigned to the int variable and we convert that int value to an Integer due to autoboxing the compiler can internally convert this automatically.

Unboxing

Unboxing is the process of automatic conversion of the object of the wrapper class into its corresponding primitive type. It is just the inverse process of autoboxing. Example:- convert Integer into an int, Byte into a byte, Character into char, Float into float, etc.

Here also the use of the intValue() method (which converts wrapper class object into primitive type) is not required.

Example:-

// Example of a java program for using wrapper class (converting wrapper class object into primitive type)
public class GeektoCode {
        public static void main(String args[]){
                    //declaring a wrapper class object
                    Integer object = new Integer(100);
                    //converting of Integer to int
                    int num = object; //here compiler can convert this wrapper class to primitive automatically
                    System.out.println(num);
        }
}

OUTPUT:-

100

In the above example, we assigned a value to a wrapper class object and convert that into an int type due to unboxing the compiler can internally convert this automatically.

Example of Wrapper class(Autoboxing) in Java

// Example of a java program for using wrapper class (converting all primitives into its corresponding wrapper class objects and vice-versa)
public class GeektoCode {
        public static void main(String args[]){
                    byte num1=100;
                    short num2=200;
                    int num3=300;
                    long num4=400;
                    float num5=500.5F;
                    double num6=600.10D;
                    char num7='A';
                    boolean num8=true;

                    //Converting of primitives type to its corresponding wrapper class objects
                    Byte num1byte=num1;
                    Short num2short=num2;
                    Integer num3int=num3;
                    Long num4long=num4;
                    Float num5float=num5;
                    Double num6double=num6;
                    Character num7char=num7;
                    Boolean num8bool=num8;

                    System.out.println("Byte value: "+num1byte);
                    System.out.println("Short value: "+num2short);
                    System.out.println("Integer value: "+num3int);
                    System.out.println("Long value: "+num4long);
                    System.out.println("Float value: "+num5float);
                    System.out.println("Double value: "+num6double);
                    System.out.println("Character value: "+num7char);
                    System.out.println("Boolean value: "+num8bool);
        }
}

OUTPUT:-

Byte value: 100
Short value: 200
Integer value: 300
Long value: 400
Float value: 500.5
Double value: 600.1
Character value: A
Boolean value: true

Example of Wrapper class(Unboxing) in Java

// Example of a java program for using wrapper class (converting wrapper class object into its corresponding primitive types)
public class GeektoCode {
        public static void main(String args[]){
            //declaring of wrapper class objects
            Byte num1obj=100;
            Short num2obj=200;
            Integer num3obj=300;
            Long num4obj = new Long(400);
            Float num5obj=500.5F;
            Double num6obj=600.10D;
            Character num7obj='A';
            Boolean num8obj=true;

            //Converting of wrapper class objects into their corresponding primitive type
            byte num1byte= num1obj;
            short num2short=num2obj;
            int num3int=num3obj;
            long num4long=num4obj;
            float num5float=num5obj;
            double num6double=num6obj;
            char num7char=num7obj;
            boolean num8bool=num8obj;

            System.out.println("Byte value: "+num1byte);
            System.out.println("Short value: "+num2short);
            System.out.println("Integer value: "+num3int);
            System.out.println("Long value: "+num4long);
            System.out.println("Float value: "+num5float);
            System.out.println("Double value: "+num6double);
            System.out.println("Character value: "+num7char);
            System.out.println("Boolean value: "+num8bool);
        }
}

OUTPUT:-

Byte value: 100
Short value: 200
Integer value: 300
Long value: 400
Float value: 500.5
Double value: 600.1
Character value: A
Boolean value: true

Key points :

  • In Java wrapper class is used to wrap the primitive data type into its class object.
  • Features like Autoboxing and unboxing are converts the primitive type into wrapper class objects and objects into their corresponding primitive types automatically.
  • In Java wrapper classes are provided within java.lang package.
  • Here we can also create our custom Wrapper classes also.

So this is all about the Wrapper class in Java. I hope this is really helpful to you. In the next upcoming post we will discuss another new topic of java, so wait for this will meet in the next post. Thank you.

Leave a Reply

Your email address will not be published.

Sorry
Sorry
Sorry
Sorry
Sorry
Sorry
Sorry