Autoboxing and Unboxing are the features included in Java 1.5, where the auto conversion happens from Primitive data type to its corresponding Wrapper Class and Vice-Versa. Autoboxing features allows you to use the primitive data type and object types interchangeably in Java in many places. The most familiar case is that when we use Collections like ArrayList or TreeSet etc.. we all know that it can take only Object data types and not primitive types, then we need to convert primitive to object type and add it to the collection. With the introduction of autoboxing and unboxing the conversion happens automatically by the Java compiler
Autoboxing: Conversion of the primitive data types to their corresponding wrapper classes is known as autoboxing. For example: conversion of long to Long, int to Integer, double to Double etc.
Unboxing: It is just the reverse process of autoboxing. Automatically converting an object of a wrapper class to its corresponding primitive type is known as unboxing. For example : conversion of Integer to int, Float to float, Double to double etc.
Autoboxing in Java
1. Adding values to the collection framework
We all know that collection can take only Object type and not primitive type, even when you add primitives to the collection, the compiler will not throw any error it will do the conversion(Autoboxing) for you. Lets take the below example.
ArrayList<Integer> arrayList1 = new ArrayList<Integer>(); arrayList1.add(1); arrayList1.add(2); arrayList1.add(3);
In the above code, the ArrayList can take only Integer type elements whereas we are adding only primitives.
2. Parameter to a Method
When a method is expecting a parameter of Wrapper class type but the parameter passed is of primitive type.
public class Test1 { public static void main(String args[]) { disp(10); } public static void disp(Integer val) { System.out.println(val); } }
Even though the disp() method expects a wrapper class(Integer) parameter, but we are passing the int(primitive type) . The program runs fine as the compiler does the Autoboxing here.
3. Assigning Primitives to Wrapper
When you are assigning a primitive type value to wrapper class of its type. For example: The below statements are completely valid
Float f = 3.0f; Integer i = 3;
Unboxing in Java
1. Retrieving value from the Collection Framework
The get() Method of the collection will return Wrapper class still you can assign it to primitives.
ArrayList<Integer> arrayList1 = new ArrayList<Integer>(); int val = arrayList1.get(0);
Here the arrayList1 returns Integer type object, which compiler unboxes and sets to int val
2. Parameter to a Method
When a method is expecting a parameter of primitive type but the parameter passed is of Wrapper class type.
public class Test1 { public static void main(String args[]) { disp(new Integer(10)); } public static void disp(int val) { System.out.println(val); } }
Even though the disp() method expects a primitive(int) parameter, but we are passing wrapper class type(Integer) . The program runs fine as the compiler does the Unboxing here.
3. Assigning Wrapper to Primitives
When you are assigning a wrapper type to primitives. For example: The below statements are completely valid
Integer wrapper = new Integer(10); int i = wrapper;
How compiler does Autoboxing and Unboxing?
We have learnt that the compiler converts the primitives to Wrapper type and vice-versa. Lets now discuss what exactly compiler does during autoboxing and unboxing.
Autoboxing
What we are able to see
Interger val = 10;
What compiler does for us
Integer val = Integer.valueOf(10);
Unboxing
What we are able to see
Integer wrapper = new Integer(10); int val = wrapper;
What compiler does for us
Integer wrapper = new Integer(10); int val = wrapper.intValue();
Leave a Reply