What does autoboxing do for you? Well, its quite helpful. You no longer have to take your primitive data types and turn them into objects when using them in a collection. Collections do not like primitive data types. Lets look at the two versions of the add() method for class ArrayList so that we can see why it shuns primitive data types:
Method Summary
add(int index, Object element)Inserts the specified element at the specified position (index) in this list.
add(Object o)
Appends the specified element to the end of this list.
Let's psychoanalyze these two methods to find out what these methods and argument types really mean.
Method One
add(int index, Object element)The first add() method will accept an int but dont get excited just yet! This int simply represents the index of the ArrayList (not an actual element in the list, its more like a counter). The second argument is Object. That means only Objects can go into an ArrayList.
Method Two
add(Object o)This simply adds another Object to the end of the ArrayList, not at any particular index.
Now that we have seen why ArrayLists cannot be filled with primitive data types, we can move on to autoboxing. Until Java 5 if you wanted to create an ArrayList full of ints, you had to first convert each int to an Integer, the wrapper class for an int. Only then could you add the values to your ArrayList. That was a hassle.
Lets look at an example of how we would deal with the issue before we had autoboxing:
import java.util.ArrayList;Whats happening in the code above? We have two ints, a and b, and an ArrayList. In order to add those ints into our ArrayList, we have to create new Integer objects. This is the only way we can stuff those ints into an ArrayList. We are putting them in an Integer box.
import java.util.Iterator;
public class BeforeAutoboxExample {public static void main(String[] args) {
int a = 121; //Notice the ints
int b = 14;
ArrayList myNumberList = new ArrayList();
myNumberList.add(new Integer(a));//We have to convert our ints to Integers
myNumberList.add(new Integer(b));Iterator i = myNumberList.iterator();
while (i.hasNext()) {
System.out.println(i.next());}
}
}
But this no longer poses a problem with Java 5. We can avoid this hassle altogether via autoboxing. Take a look at the following code:
import java.util.ArrayList;The code above uses both generics (generics are indicated by the less than/greater than symbols) and autoboxing. We still have our two ints, a and b. But we have defined our ArrayList to accept Integers in the following line:
import java.util.Iterator;
public class AutoboxExample {public static void main(String[] args) {
int a = 121;
int b = 14;
ArrayList<Integer> myNumberList = new ArrayList<Integer>();
myNumberList.add(a);
myNumberList.add(b);Iterator i = myNumberList.iterator();
while (i.hasNext()) {
System.out.println(i.next());}
}
}
ArrayList<Integer> myNumberList = new ArrayList<Integer>();Essentially, this tells the compiler to expect values that are of class Integer. When you add the ints a and b to myNumberList, the ints are converted, or boxed, into Integers. The nice thing is that they still remain as ints. No permanent damage has been done. Notice how easy it is to add elements to the list, even though they are actually ints:
myNumberList.add(a);So, if you are a developer accustomed to the ways of Java 1.4 and you're using Java 5 or Java 6, you may run into some unusual issues with data type checking. Check around your code for areas where autoboxing should work for you and consider rewriting some of your code. Remember, autoboxing is your friend.
myNumberList.add(b);
