What is Auto Boxing?

Consider the following piece of Java code.

AutoBoxing

public class AutoBoxing
{
    public static void main(String[] args)
    {
        Integer intWrapper = new Integer(100);
        intWrapper++;
        System.out.println("intWrapper = " + intWrapper);
    }
}

Output

intWrapper = 101

In this example, the Integer Wrapper object is created with integer value set as 100;
The Wrapper Object is incremented as intWrapper++ and its value changed to 101. Here, Java is taking care of un-boxing, increment the value and then re-assign the incremented value to the Integer Wrapper Object. This is called Auto Boxing.

This is introduced in Java 5.

Interview Questions: 

Search