Well, it has been noticed that all the primitive data types like int, float, double, etc in Java are dealt with differently. Even after the good performance offered by the primitive data types, there are circumstances when we might need an object representation of the primitive data type. Moreover, it has been witnessed that many data structures in Java operate on objects. So we cannot use primitive data types with those data structures. To handle such types of situations, Java can provide the type “Wrappers” which provide classes that encapsulate the primitive type within the object. This led to the emergence of “Wrapper Classes” which involve two ingredients playing an active role including “Autoboxing” and “Unboxing”.
What is Autoboxing?
In simpler words, we can say “Autoboxing” is the process involving “automatic conversion” which is done by the Java compiler between the “primitive data types” and “their corresponding object wrapper classes”.
From the above example it can be identified, that conversion of int type to Integer object or char type to Character object. This conversion is being done implicitly by the Java compiler during the program execution.
Occurrence of Autoboxing
It has been witnessed that Java Compiler bids “Autoboxing” under three cases:
The first case takes place when we try to pass a “primitive data type as a parameter to a method”. However, the method looks for an “object of the wrapper class” which is in relation to the “primitive data type”.
The occurrence of ”Autoboxing” takes place when we allocate a ‘primitive data type” to a variable ” that corresponds to the “Wrapper class”.
The most vital case includes the usage of “Collection framework classes” where the compiler is responsible for performing “Autoboxing” in Java. Like in the ArrayList framework the class looks for an object of the “Integer Wrapper class” but if we pass the int primitive data type, the compiler headily converts the “int data type” into the object of the “Integer Wrapper class”.
What is Unboxing?
The process where the Java compiler headily converts the “Wrapper class objects” into the corresponding primitive data types is known as “Unboxing”. In other words, “Unboxing” is the complete opposite of “Autoboxing”. For example conversion of Integer class to int data type, as shown in the image below.
Unboxing in Java
In the above picture, the get() method is responsible for returning the object at index 0. However, because of “Unboxing”, the object gets automatically transformed into primitive data type int and is allocated to the variable “a”.
Autoboxing and Unboxing in Method
In the above picture, we can see the way “autoboxing and unboxing” is taking place in a method. While during method call we have passed int type primitive value but method accepts only Integer objects so here JVM does “autoboxing” implicitly. On the other hand, this method returns an int type primitive which is “unboxing” from Integer to int type.
Advantages of Autoboxing and Unboxing
l Both the techniques help the developers to write cleaner code which makes it easier to read.
l With the help of both the techniques we are able to utilize primitive types as well as “Wrapper class objects” interchangeably. This is advantageous as developers are not required to perform typecasting “explicitly”.
l It is useful as it prevents errors. However, sometimes it may lead to unexpected results, therefore, must be used with care.