Autoboxing in Java 5

Autoboxing feature in Java 5 is to make code look neater and cleaner. Autoboxing, as name suggests, does something automatically. It converts data types from one form to other automatically. This does not apply to any data type but to some selected data types which allow this conversion (generally without losing out any information). Using autoboxing, primitive data type values can be converted into respective wrapper objects and vice versa. Take a look at some of the data types that can avail this feature of autoboxing.

	int <-- --> Integer
	boolean <-- --> Boolean
	long <-- --> Long
	....
	....

Let us take an example of code prior to this feature and compare it with code written using autoboxing to understand it better.

//Old Code

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class WithoutAutoboxing {

	public static void main(String[] args) {
		List list = new ArrayList(5);
		list.add(new Integer(1));
		list.add(new Integer(2));
		list.add(new Integer(3));
		list.add(new Integer(4));
		list.add(new Integer(5));

		for(Iterator itr = list.iterator(); itr.hasNext();){
			System.out.println("value is: " + ((Integer)itr.next()).intValue() + 1);
		}
	}

}

We can add only objects to an ArrayList, hence we need to wrap the int values in Integer wrapper class and add to the list. While retrieving the values from ArrayList, we have to again cast the Object returned to Integer. We have to increment it by 1, hence again we get intValue() of it and add 1 to it. Each value is converted thrice in this operation, and we have to write code to do this. Life won’t be so simple in practical cases. We may have far more number of attributes getting into lists and coming out. Just imagine how much additional code clutter we would be creating if there is similar requirement.

The solution is use of autoboxing. You can see the change in code clearly below.

//New Code

import java.util.ArrayList;
import java.util.List;

public class WithAutoboxing {

	public static void main(String[] args) {
		List<Integer> list = new ArrayList<Integer>(5);
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(4);
		list.add(5);

		for(int num: list){
			System.out.println("value is: " + num + 1);
		}
	}

}

In this example, we have also used two other features of Java 5 – generics and for-each loop. But the feature under discussion – autoboxing can be seen in operation clearly. At following steps, we can see autoboxing in above code.

In this example, generics define the kind of object going in the list but it also tells compiler which Wrapper object is to be used if any primitive data is added to the list. Hence if you try to add any longer value to the list the compiler will generate an error. Also if you do not use generics and allow any value to be added to the list, then there will not be autoboxing in operation.

Important Considerations While Using Autoboxing:

Finally, autoboxing feature does improve readability of code, but the free casting has performance implications. Hence it should be used judiciously.

Previous Next

  • Share/Bookmark
Java, Tech Notes

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Leave Comment

(required)

(required)