Home > DeveloperSection > Articles > Autoboxing and Unboxing in Java

Autoboxing and Unboxing in Java

Java Java 
0 Comment(s)
 221  View(s)
Rate this:

Autoboxing/Unboxing in Java

In the previous posts, we have just seen the use of type wrappers in wrapping the primitive data types. Beginning in J2SE 5.0, we will not have to explicitly perform such wrapping. The wrapping/unwrapping is now implicit and automatic.

For example, to wrap an integer constant 100 into an Integer type, prior to J2SE 5.0 you would use the following code:

Integer a = new Integer(100);

Now, with the autoboxing feature introduced in J2SE 5.0, we can achieve the same with the following code:

Integer a = 100;                                 

Here, the number 100 is encapsulated into an Integer type and assigned to the variable a. There is no need to use the new keyword. To unwrap the contents of Integer, prior to J2SE 5.0, we would have used the following statement:

int i = a.intValue();

Now, we would simply type the following:

int i = a;

Very neat and clean, isn’t it?

The process of wrapping a primitive data type into its corresponding type wrapper class is called auto-boxing, and the opposite process of extracting the wrapped primitive type from an object is called unboxing.

This implicit boxing/unboxing works in all situations wherever such an action is necessary. For example, we can freely mix the objects and the primitive types in an arithmetic expression. Consider the following code fragment:

Integer a = new Integer(100);

int b = 200;

Integer c = a + b;



This code compiles without errors and produces the expected result of c equals 300. Note that the expression a + b adds an int variable and an Integer object. In this case, the Integer object a is implicitly converted to an int type. The program then performs the addition of two int data types and assigns this to variable c of type Integer. During assignment, the autoboxing converts the int type to the Integer type.

The autoboxing/unboxing also applies to method parameters and the return type. For example, consider the following method declaration:

private static Integer adder(Integer a, Integer b) {

return a + b;




The method takes two arguments of type Integer and returns an Integer result. We may call this method with the following statement:

int result = adder (100, 200);

We are passing int-type parameters rather than Integer objects to the method. Inside the method, these parameters are converted to Integer objects. In the evaluation of the expression a+b, the objects are unboxed into int types. The return statement boxes the result into an Integer type. The returned value is unboxed and assigned to result, which is an int type variable.

This entire process is demonstrated in the trivial example shown here.

public class Autobox {

                     public static void main(String args[]) throws Exception {

                             System.out.println("Demonstrating power of autoboxing/unboxing");

                             Integer a = 100;

                             int b = 200;

                             int c = a + b;

                             System.out.println("Autoboxing in action: arithmetic expressions");

                             System.out.printf("%d + %d = %d%n%n", a, b, c);

                             System.out.println("Autoboxing in action: "

                                                + "method parameters and return types");

                             System.out.printf("%d + %d = %d%n", a, b, adder(a, b));



                     private static Integer adder(Integer a, Integer b) {

                             return a + b;




The autoboxing/unboxing feature now frees us completely from using the tedious wrapper classes.

Don't want to miss updates? Please click the below button!

Follow MindStick