Java Autoboxing and Unboxing

Beginning with the JDK 5, Java added the following two important features:

  • autoboxing
  • auto-unboxing

Java Autoboxing

Autoboxing is a process through which a primitive type is automatically encapsulated (boxed) into its equivalent type wrapper whenever an object of that type is required. There is no need to explicitly construct an object.

Java Auto-unboxing

Auto-unboxing is a process through which the value of a boxed object is automatically extracted (unboxed) from type wrapper when its value is required. There is no need to call a method such as doubleValue() or intValue().

The addition of the autoboxing and auto-unboxing greatly streamlines the coding of several algorithms, removing the tedium of manually boxing and unboxing the values. It also helps to prevent errors. Moreover, it is very important to generics, which operate only on objects. Now, autoboxing makes working with the Collections Framework

With the autoboxing, it is no longer necessary to manually construct an object in order to wrap a primitive type. You require only to assign that value to a type-wrapper reference. Java automatically constructs the object for you. For example, here is a modern way to construct an Integer object that has the value 10:

Integer iObj = 10;    // autobox an int

Notice here that the object is not explicitly created through the use of the new keyword. Java handles this for you, automatically.

How to Unbox an Object ?

To unbox an object, just assign that object reference to a primitive type variable. For example, to unbox the iObj, you can use the following statement:

int i = iObj;   // auto-unbox

Java handles the details for you.

Java Autoboxing Unboxing Example

Following is the preceding program (rewritten) to use autoboxing/unboxing:

/* Java Program Example - Java Autoboxing
 * This program demonstrate autoboxing/unboxing */
 class AutoBoxingDemo
     public static void main(String args[])
         Integer iObj = 10;    // autobox an int
         int i = iObj;         // auto-unbox
         System.out.println(i + "  " + iObj);    // displays 10  10

Above Java program will produce this output:

java autoboxing

Java Autoboxing and Methods

In addition to simple case of assignments, autoboxing automatically occurs whenever a primitive type must be converted into an object, auto-unboxing takes place when an object must be converted into a primitive type. Thus, autoboxing/unboxing may occur when an assignment is passed to a method, or when a value is returned by a method. For example, consider the following example program:

/* Java Program Example - Java Autoboxing/Unboxing
* Autoboxing/unboxing takes place with 
* method parameters and return values    */
 class AutoBoxingDemo
     /* takes an integer parameter and return
      * an int value      */
      static int meth(Integer val)
          return val;        // auto-unbox to int
     public static void main(String args[])
         /* pass an int to the method named meth() 
        * and assign the return value to an Integer.
        * Here, the
          * argument 10 is autoboxed into an Integer.
          * The return value is also autoboxed into
          * an Integer          */
          Integer iObj = meth(10);

Above Java program will display the following output:

java autobox unbox

In this program, notice that, meth() specifies an Integer parameter and returns an int result. Inside the main() method, the method meth() is passed the value 10. Because meth() is expecting an Integer, this value is automatically boxed. Then, the method named meth() returns the int equivalent of its argument. This causes val to be auto-unboxed. Next, this int value is assigned to iObj in the main() method, which causes the int return value to be autoboxed.

Java Online Test

« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook

Subscribe Us on YouTube