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.
- Autoboxing: While adding int to Arraylist, the conversion in Integer happens automatically.
- Unboxing: While retrieving the value, it can be directly retrieved in int value.
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:
- Performance: Whenever we convert primitive values in object or vice versa, there is a cost involved. If the casting is happening multiple times then there will be performance degradation. Hence before allowing autoboxing to work, just check if you can avoid the casting business by selecting correct data type first.
- Null Values: Wrapper object classes can have ‘null’ value while the primitive values cannot. Consider this while using autoboxing otherwise there will be NullPointerException if a null value is tried to autobox to a primitive variable.
- Loss of Information: Integer is not exactly equivalent of int and vice versa. Same is applicable to other data types and their respective wrapper classes. Hence it may result in loss of information cause autoboxing is blurring the difference in these two data types.
- Memory Consumption: Primitive variables and Object variables are not stored at same area of physical memory. Hence conversion amongst these two leads to creation of new objects which is an additional load to garbage collection. This is an additional point to consider while using autoboxing in applications requiring high performance.
Finally, autoboxing feature does improve readability of code, but the free casting has performance implications. Hence it should be used judiciously.
Previous Next
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.
