Autoboxing & Unboxing of Wrapper classes

Wrapper class, Autoboxing and Unboxing

Wrapper class:

In java if we want to work on various data types ,we have primitives(int, char, long etc) types available but if you want to work only with objects then wrapper classes can be helpful.

Wrapper class is a class in java which is a wrapper around primitive data type.Wrapper classes encapsulate primitive data types so that we can use them instead of primitive types when we need objects.

It allows us to treat primitive(int,long,double) types as objects.Like wrapper class for int is Integer,Float for float and Double for double.

Each wrapper class takes primitive type as constructor to convert from primitive to corresponding wrapper object.e.g 

int i = 2;

Integer integerObject = new Integer(i);

Autoboxing:

Autoboxing refers to automatically converting primitive data type to corresponding wrapper object means we do not explicitly need to convert.Compiler will automatically understand this conversion.

Without autoboxing prior releases(java 5) needed explicit manual conversion for the same.Conversion of int to Integer ,double to Double.

Unboxing:

This is the reverse process of Autoboxing.It converts back object of wrapper class to corresponding primitive type.Conversion of Integer to int,Double to double etc.

AutoBoxing Example:

import java.util.ArrayList;
class AutoBoxingExample {
    public static void main(String[] args) {
        int a = 90;
        // Autoboxing - primitive type to wrapper (Integer) conversion automatically
        Integer wrapperInteger = Integer.valueOf(a);
        ArrayList arrayList
                = new ArrayList();
        // Autoboxing again since arraylist is of type wrapper (Integer) and stroing primitive type value 90
        arrayList.add(a);
        System.out.println(arrayList.get(0));
    }
}

Unboxing Example:

import java.util.ArrayList;
class UnboxingExample {
    public static void main(String[] args) {
        Integer wrapperInteger = 190;
        // Unboxing - wrapper (Integer) object to primitive type conversion automatically
        int a = wrapperInteger;
        System.out.println(a);
        ArrayList arrayList
                = new ArrayList();
        arrayList.add(wrapperInteger);
        // Unboxing again - wrapper (Integer) object to primitive type conversion automatically
        int b = arrayList.get(0);
        System.out.println(b);
    }
}

Wrapper class example:

class WrapperExample {
    public static void main(String[] args) {
        int a = 10;
        Integer intWrapper = a;
        float b = 18.6f;
        Float floatWrapper = b;
        double d = 250.5;
        Double doubleWrapper = d;
        System.out.println("Integer wrapper object: " + intWrapper);
        System.out.println("Float wrapper object: " + floatWrapper);
        System.out.println("Double wrapper object: " + doubleWrapper);
        int i = intWrapper;
        float f = floatWrapper;
        double x = doubleWrapper;
        System.out.println("primitive int value: " + i);
        System.out.println("primitive float value: " + f);
        System.out.println("primitive double value: " + x);
    }
}

2 thoughts on “Autoboxing & Unboxing of Wrapper classes”

Leave a comment