Autoboxing and Unboxing is a feature that is achieved with the help of Wrapper Classes. The wrapper class provides the mechanism to convert primitive data types into objects and objects into primitive data types.
Autoboxing
- This feature convert primitives into objects.
- In other words, it allows the automatic conversion of primitive data types into their corresponding wrapper class.
//Converting int into Integer explicitly
int num=10;
Integer i=Integer.valueOf(num);
//To achieve it using autoboxing
int num = 10;
Integer i=num;
Unboxing
- This feature converts objects into primitives.
- In other words, it allows the automatic conversion of wrapper type into its corresponding primitive type.
//To convert Integer to int explicitly
Integer i=new Integer(3);
int num=i.intValue();
//To achieve it using unboxing
Integer i=new Integer(3);
int num=i;