article cover image

What is Autoboxing and Autounboxing in JAVA?


Autoboxing and Autounboxing is concept related to wrapper classes and primitive data types in java.

 

Until Java 1.4 version we can’t use primitives and Wrapper class of that primitive interchangeably i.e. we can’t use wrapper classes at the place of primitives and primitives at the place of Wrapper class.

 

Eg. Integer i  = 10;

 

If we compile above line in Java 4 then it will give compile time error saying incompatible types. Here compiler is not able to convert int data type (primitive) value into Integer which is Wrapper class.

 

Also if we write,

 

int i = new Integer(10);

 

Then also compiler will complain about incompatible types as the compiler is not able to convert Integer(Wrapper) to int (primitive).

 

So what was the solution in Java 4?

Java provides 2 special methods in all the Wrapper classes to convert Wrapper into primitives and vice versa. As a developer, we have to use that two methods explicitly whenever we want to convert Wrapper into primitives or primitives into Wrapper.

 

Which are those methods? How to use it?

  1. valueOf() method :

This is the static method present in all the wrapper classes. This method can convert primitives or String value into corresponding Wrapper object.

 

Eg. Integer i = Integer.valueOf(10);

 

As 10 is primitive int value and we want to convert it into Integer object which is Wrapper class. Here we have to use valueOf() method present in Integer class which will convert int to Integer as shown in above example.

 

 

  1. xxxValue() method:

  If you want to convert Wrapper object to primitive then you should use “xxxValue()” method.

 

  Are you confused with “xxx”  in xxxValue() method? Let me explain, I have written xxx for general representation of all primitives. i.e. It     will become intValue() for int , byteValue() for byte, charValue() for char type and so on…

 

Let’s take an example.,

 

Integer i = new Integer(10);

 

And now we want to convert this Integer wrapper to an int primitive. It's simple,

 

Integer i = new Integer(10);

int j  = i.intValue();

 

Yup… we have successfully converted the Wrapper object into int value using intValue() method of Integer Wrapper class.

 

Similar conversions can be done for other Wrapper classes and primitives as well.

 

 

But, It's unnecessary work for a developer to convert it from Wrapper object to primitive and primitive to Wrapper Object every time.

 

Developer at SUN thought of this and as they always try to take care of there developers, they came up with the amazing concept of Autoboxing and Autounboxing in Java 5.

 

So what it provides and how it will reduce the overhead of Java developers.?

Autoboxing:

Autoboxing enables automatic conversion of primitive to Wrapper object. i.e. from Java 5 onwards compiler will convert “int” to “Integer” automatically behind the scene. This is called Autoboxing.

 

For example, if we write,

 

Integer i = 10;

 

The compiler will not give any error instead, it will convert int value 10 to Wrapper object i.e. Integer.

 

So we as a developer we don’t have to convert it using Integer.valueOf(10). The compiler will do this for us behind the scene...

Now let's take a look at Autounboxing.

Auto-unboxing:

Auto-unboxing enables automatic conversion of Wrapper object to primitive. i.e. from Java 5 onwards compiler will convert “Integer” to  “int” automatically behind the scene. This is called Autounboxing.

 

For example, if we write,

 

int i = new Integer(10);

 

The compiler will not give any error instead, it will convert Integer Object having value 10 to int value automatically.

 

So we as a developer we don’t have to convert it using intValue() method as we did in the Java 4 example. The compiler will do this for us behind the scene...

 

Conclusion:

Until Java 1.4 we can’t provide wrapper object in the place of primitive and primitive in the place of Wrapper object. If there is a need for such conversion then as a developer we have to perform such conversions explicitly.

 

But from 1.5 version onwards we can provide primitive value in the place of Wrapper object and Wrapper object in the place of primitives. Here all the required conversions will be performed automatically by the compiler. These automatic conversions are called Autoboxing(primitive to Wrapper Object) and Autounboxing (Wrapper object to primitive).

 

In the next post we will look for the corner cases for Autoboxing and Autounboxing which will prepare you for the interview questions on this topic.. See you then... Happy Learning...


0

Most Popular