Java Data Type Tutorial - Java Autoboxing and Unboxing

AutoBoxing and unboxing work with primitive data types and their corresponding wrapper classes.

They are implemented completely in the compiler.

The automatic wrapping from a primitive data type (byte, short, int, long, float, double, char and boolean) to its corresponding wrapper object (Byte, Integer, Long, Float, Double, Character and Boolean) is called autoboxing.

The reverse, unwrapping from wrapper object to its corresponding primitive data type, is called unboxing.

With autoboxing/unboxing, the following code is valid:

Integer n  = 2;  // Boxing 
int a  = n;        // Unboxing

The compiler will replace the above statement with the following:

Integer n  = Integer.valueOf(2);
int a  = n.intValue();