AutoBoxing and Auto-unboxing in Java
|Two features that were a part of new features list of JDK5 are Autoboxing and Auto-unboxing.
Autoboxing is the process of automatically encapsulating a primitive data type into an object.
Auto-unboxing refers to the process of automatically extracting the value from the type wrapper without the use of functions such as intValue() or charValue().
We can simply use the following code to create an object of the primitive data type:
Integer iOb = 5; //autoboxing int Character chOb = 'A'; //autoboxing char Double dOb = 3.14; //autoboxing double
We do not need to use the new keyword to create an object. It is automatically done by Java, thus Autoboxing Similarly, Auto-unboxing can be simply done by the following way:
int i = iOb; //auto-unboxing the int type char ch = chOb; //auto-unboxing the char type double d = dOb; //auto-unboxing the double type
Using Expressions to Autobox and Auto-unbox
The best example of Autoboxing and Auto-unboxing is when we are using expressions which deal with primitive types. In an expression, a numeric type is automatically unboxed, the operation is performed and then the object is automatically boxed into an object again. The following example shows this:
Unable to retrieve the Code part. Please reload again. Notify us if the problem still persists. Till we work on this you can view code on URL below. Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/Autobox_Unbox/AutoboxExpDemo.java
Output:-
Value of iOb = 56
Value of iOb++ = 57
Final Value of iOb = 3420
Using Methods to Autobox and Auto-unbox
Auto-unboxing can automatically occur when an object needs to be converted into its primitive type, eg: when returning an object if the return type is a primitive type and if an object that is passed as an argument is returned. Also, if any method returns a primitive data type and that return value is assigned to an object then Java automatically boxes it into an object. The following example shows Autoboxing and Auto-unboxing in methods:
Unable to retrieve the Code part. Please reload again. Notify us if the problem still persists. Till we work on this you can view code on URL below. Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/Autobox_Unbox/AutoboxMethodDemo.java
Output:-
Enter a number :
123
The reverse of the number is : 321
Objects to control Switch statements and if-else statements
The Integer objects can be used to control the switch statement easily with the help of Auto-unboxing. The object is passed as the switch condition and the compiler automatically unboxes the object to extract the int data which is then used as the condition. An example is shown below:
Unable to retrieve the Code part. Please reload again. Notify us if the problem still persists. Till we work on this you can view code on URL below. Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/Autobox_Unbox/AutoboxSwitchDemo.java
Output:-
Enter the serial number of the day you wish to see :
4
Wednesday
We know that if takes boolean as the control type. With the help of Auto-unboxing, it is possible to pass a Boolean object in the place of the condition to make the if statement work. This is shown below:
Unable to retrieve the Code part. Please reload again. Notify us if the problem still persists. Till we work on this you can view code on URL below. Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/Autobox_Unbox/AutoboxIfDemo.java
Output:-
bOb contains True
Mixed Type expressions using objects
While performing operations on objects it is completely valid to perform operations between mixed type data as a programmer would have done with normal primitives. The objects are automatically unboxed, operations are performed and then the objects are automatically boxed into objects again. An example is shown below:
Unable to retrieve the Code part. Please reload again. Notify us if the problem still persists. Till we work on this you can view code on URL below. Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/Autobox_Unbox/AutoboxMixExpDemo.java
Output:-
Value of iOb = 66
Value of dOb = 30.0
Final Value of dOb = 2.2
Advantages of Autoboxing and Auto-unboxing
- Autoboxing and Auto-unboxing removes the user’s need to manually construct an object to wrap a primitive data type
- It is important when implementing the concepts of generics and collection framework. It is because we can not use a primitive type in Collections or Generics because for Example HashMap<Integer, Integer> is valid and not HashMap<int, int>.
- It helps to prevent errors.
- It makes the coding of many algorithms well organized.
Note: Although Autoboxing and Auto-unboxing make dealing with the objects as simple as dealing with actual primitives, it should be kept in mind that objects should be used only where it is required. Using objects in place of primitives can add overhead which should be avoided.
Learning never exhausts the mind.So, do come back for more. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. Share and subscribe.
Keep Coding!! Happy Coding!! 🙂