Autoboxing Java


Autoboxing Java – Automatic Conversion

Java data structures always (in all the JDK versions), stores only objects and when retrieved return only objects. The problem is, even the simple primitive data types (used very often) are to be converted into objects (using wrapper classes) and then stored. The retrieved objects from the DS are to be converted back to original data types to be used in arithmetic operations later. This is a big headache to the programmer. This is overcome in JDK 1.5 with the introduction of autoboxing concept. Autoboxing Java permits to store primitive data types directly in DS and retrieve back in data types form. Let us discuss more elaborately.

See the following code. ArrayList was introduced in Java 2 (JDK 1.2). An ArrayList object al is created and added with an element of marks. Nothing more is done.

Example on Autoboxing Java
import java.util.ArrayList;
public class Demo
{
  public static void main( String args[])
  {
    ArrayList al = new ArrayList();     
    int marks = 50;
    al.add(marks);                     
  }                                    
}

Autoboxing Java

Observe the error message raised by the compiler of JDK 1.4. Upto JDK 1.4, primitive data types cannot be added to a DS of Java. They must be explicitly converted into objects using wrapper classes and then added. To work with the above code successfully, following modification should be done.

ArrayList al = new ArrayList();
int marks = 50;
Integer i1 = new Integer(marks);
al.add(i1);

Let us see when retrieved what to be done.

import java.util.ArrayList;

public class Demo
{
  public static void main( String args[])
  {			                         // addition of an element
    ArrayList al = new ArrayList();     
    int marks = 50;             
    Integer i1 = new Integer(marks);
    al.add(i1);
			                         // retrieval of an element
    Object obj = al.get(0);
    Integer i2 = (Integer) obj;
    int marks1 = i2.intValue();
    System.out.println(marks1*marks1);           // prints 2500
  }
}

Object obj = al.get(0);
Integer i2 = (Integer) obj;
int marks1 = i2.intValue();
System.out.println(marks1*marks1); // prints 2500

The get() method returns an object of Object class(here, it is obj). Explicit cast to Integer, i2. Get back primitive data type marks1 using intValue() method. Don’t you feel it is all round about process?

Forget all this laborious process from JDK 1.5. From JDK 1.5, the compiler itself converts the primitive data type into object and then stores. When retrieved, it returns primitive data type. Converting primitive data type to object implicitly is known as autoboxing and again converting object to data type is known as unboxing. Autoboxing and unboxing were introduced with JDK 1.5 as a new feature and made programmer life easier.

Let us rewrite the above code and compile with JDK 1.7 version.

import java.util.ArrayList;

public class Demo
{
  public static void main( String args[])
  {			                           // addition of an element  
    ArrayList al = new ArrayList();     
    int marks = 50; 
    al.add(marks);                                 // int marks implicitly converted to object and then stores
			                           // known as Autoboxing Java
    int marks1 = al.get(0);                        // retrieval of an element
    System.out.println(marks1*marks1);             // prints 2500
  }
}

Autoboxing Java

Here, another feature of JDK 1.5 generics is used. Now observe, primitive data type marks is added and retrieved as primitive data type marks1. This automatic conversion to object is known as autoboxing and getting back to data type is known as unboxing.

Apart autoboxing, two other features added to DS from JDK 1.5 are generics and enhanced for loop.

Leave a Comment

Your email address will not be published.