Java

Java – Autoboxing And Unboxing With Examples

Java 1.5 included a unique capability that automatically converts basic types to the appropriate Wrapper class and vice versa.

Autoboxing is the process of automatically converting primitive types into the objects of the wrapper classes that correspond to them. As an example, convert an integer to an integer, a long to a long, a double to a double, etc.

Unboxing: This is just autoboxing done backwards. Unboxing is the process of automatically transforming a wrapper class object to its equivalent primitive type. As an example, convert an integer to an integer, a long to a long, a double to a double, etc.

Primitive type	Wrapper class
boolean	        Boolean
byte	        Byte
char	        Character
float	        Float
int	        Integer
long	        Long
short	        Short
double	        Double

When does the autoboxing and unboxing happens in Java

Autoboxing

Let’s look at a few instances of autoboxing with examples.
Case 1: When a method expects a wrapper class object but receives a value of a primitive type instead. For instance, the method myMethod() in the code below expects an object of the Integer wrapper class, but we passed a primitive int type instead. As the compiler performs the autoboxing, the program worked as intended.

class AutoboxingExample1
{
   public static void myMethod(Integer num){
	System.out.println(num);
   }
   public static void main(String[] args) {
       /* passed int (primitive type), it would be 
        * converted to Integer object at Runtime
        */
   	myMethod(2);
   }
}

Output:

2

Case 2:when you sometimes provide a primitive type value to an object that belongs to its wrapper class. As an example, the following sentences are correct since the compiler does autoboxing at runtime.

Integer inum = 3; //Assigning int to Integer: Autoboxing
Long lnum = 32L; //Assigning long to Long: Autoboxing

Case 3: When working with classes from the collection framework:

ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(11); //Autoboxing - int primitive to Integer
arrayList.add(22); //Autoboxing

Here ArrayList class is expecting an Integer wrapper class object but we are providing int primitive.

Unboxing

 

Case 1:The Integer object (parameter) that the method was expecting was not provided. Integer was automatically changed to an int during unpacking.

class UnboxingExample1
{
   public static void myMethod(int num){
	System.out.println(num);
   }
   public static void main(String[] args) {
    	
    	Integer inum = new Integer(100);
    	
        /* passed Integer wrapper class object, it 
         * would be converted to int primitive type 
         * at Runtime
         */
    	myMethod(inum);
    }
}

Output:

100

Case 2: Assignments

Integer inum = new Integer(5);
int num = inum; //unboxing object to primitive conversion

Case 3: As it relates to collection classes

ArrayList arrayList = new ArrayList()
int num = arrayList.get(0); // unboxing because get method returns an Integer object

What happens behind the scenes?

We learned how the Java compiler automatically converts between primitive types and matching Wrapper objects in the section above. Let’s talk about the actual autoboxing and unpacking processes that the compiler uses. The easiest way to comprehend this is to contrast the state of affairs before and after Java 1.5. (boxing and unboxing introduced in java 1.5).

Autoboxing:
We observe:

Integer number = 100;

What the compiler accomplishes (or what we did prior to Java 1.5):

Integer number = Integer.valueOf(100);

Unboxing:
We observe:

Integer num2 = new Integer(50);
int inum = num2;

How a compiler works

Integer num2 = new Integer(50);
int inum = num2.intValue();

Few things you should take care:

When doing comparisons, keep primitives and objects separate. For some comparisons, the outcomes might be uncertain. The better course of action is to compare primitives with primitives and compare objects with objects (using the equals() function and logical operators like “==”, “,” etc.).

Related Articles

Leave a Reply

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

Back to top button