Nov 30, 2014

Autoboxing and Unboxing - Java



New feature has been added to Java 5 known by various names: autoboxing, auto-unboxing, boxing, and unboxing. Understanding the concept of Autoboxing requires knowledge of Wrapper classes. Stop here and read this first before proceeding further if you do not have an idea of Wrapper classes.

Now that you are aware of wrapper classes, you must know that wrapper classes makes life so much easier of a programmer, giving them ways to perform operation on primitive types that are reserved for Objects, like adding to Collections. Boxing and unboxing make using wrapper classes more convenient. 


Prior to Java 5, using an information within a wrapper and then rewrapping it would require following sequence of actions:

Integer wrapper = new Integer(111); // make a wrapper
int value = y.intValue(); // unwrap it and get the value
value--; // use the value
wrapper = new Integer(value); // re-wrap it

Now, Java 5 onwards, it can be done with just 2 statements

Integer wrapper = new Integer(111); // make a wrapper
wrapper--; // unwrap it, use it, rewrap it

If you see closely, postdecrement operator is being applied on the object reference variable directly. Compiler does the unboxing and reassignment.

But important point to note here is that wrapper objects are immutable. So, rewrapping the information would result in separate object creation. Things which users need to perform manually before Java 5, happen automatically now.

It can be verified from following example:

Integer wrapper = new Integer(111); // make a wrapper
Integer wrapperCopy = wrapper; // create a copy
wrapper--; // unwrap it, use it, rewrap it

System.out.println(wrapper==wrapperCopy); // It will print false // confirming that they refer to different objects

It’s important to know that how wrappers behave with ==, != and equals(). Can be little confusing. For wrapper classes, two objects are considered equal if they are of the same type and have the same value.

Integer wrap1 = 1111;
Integer wrap2 = 1111;
Integer wrap3 = 11;
Integer wrap4 = 11;

if(wrap1 != wrap2) System.out.println("wrap1 and wrap2 are different objects");
if(wrap1.equals(wrap2)) System.out.println("wrap1 and wrap2 are equal");
if(wrap3 == wrap4) System.out.println("wrap3 and wrap4 are same object");
if(wrap3.equals(wrap4)) System.out.println("wrap3 and wrap4 are equal");

Output:
wrap1 and wrap2 are different objects
wrap1 and wrap2 are equal
wrap3 and wrap4 are same object
wrap3 and wrap4 are equal

“equals()” seems to be working as expected, yielding true when content for the wrapper object are equal. But, what happening with the == and != operators. yielding contradictory results. Reason for this is two instances of the following wrapper objects (created through boxing), will always be == when their primitive values are the same:
  • Boolean
  • Byte
  • Character from \u0000 to \u007f
  • Short and Integer from -128 to 127
When == is used to compare a primitive to a wrapper or vice-versa, the wrapper will be unwrapped and the comparison will be primitive to primitive.

0 comments:

Post a Comment