Autoboxing and Unboxing in Java

Autoboxing and Unboxing in Java

Before understanding autoboxing and unboxing in java we must know what is wrapper class. So lets take a look on it.

Wrapper Class

A wrapper class wraps around a primitive data type and gives it an object appearance. This wrapper class object can be used whenever the primitive data type is required as an object.

Autoboxing in Java

The process of converting primitive data type into wrapper
class object automatically is known as autoboxing. This feature was added from JDK
1.5.
The example for autoboxing is given below. Till JDK 1.4 we
have to manually do the conversion of primitive data type into wrapper class
and vice-versa using wrapper class method but from JDK 1.5 it is done
automatically.

Till JDK 1.4

int x=10;
Integer i=new Integer(x);  //boxing

From JDK 1.5

int x=10;
Integer i=x;         //autoboxing

Unboxing in Java

The process of converting wrapper class object into
primitive data type automatically is called unboxing.

Till JDK 1.4

int x=10;
Integer i=new Integer(x);   //boxing
int z=i.intValue();         //unboxing

From JDK 1.5

int x=10;
Integer i=x;         //autoboxing
int z=i;             //unboxing

A list of primitive data type with their corresponding
wrapper class is given below.
Primitive
Data Type
Wrapper
Class
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character
boolean
boolean
Autoboxing and Unboxing in Java (Wrapper Class)

Video Tutorial to Explain Autoboxing and Unboxing in Java

If you found anything wrong or missing in above tutorial then please mention it by commenting below.

Leave a Comment

Your email address will not be published. Required fields are marked *