Saturday, January 30, 2016

Topic 33: Autoboxing in Java

  • Autoboxing is the process by which a primitive type is automatically encapsulated (boxed) into its equivalent type wrapper whenever an object of that type is needed. There is no need to explicitly construct an object. 

  • Auto-unboxing is the process by which the value of a boxed object is automatically extracted (unboxed) from a type wrapper when its value is needed. There is no need to call a method such as intValue( ) or doubleValue( ).

  • With autoboxing it is no longer necessary to manually construct an object in order to wrap a primitive type. We need only assign that value to a type-wrapper reference. Java automatically constructs the object for us. For example, here is the modern way to construct an Integer object that has the value 100:

Integer iOb = 100; // autobox an int

Notice that no object is explicitly created through the use of new as discussed earlier in Type Wrappers. Java handles this for us, automatically.

  • To unbox an object, simply assign that object reference to a primitive-type variable. For example, to unbox iOb, you can use this line:

int i = iOb; // auto-unbox

  • Autoboxing automatically occurs whenever a primitive type must be converted into an object; auto-unboxing takes place whenever an object must be converted into a primitive type. Thus, autoboxing/unboxing might occur when an argument is passed to a method, or when a value is returned by a method. For example, consider this example:

// Take an Integer parameter and return an int value;
static int m(Integer v)
{
return v ; // auto-unbox to int
}


//Inside main() - Pass an int to m() and assign the return value to an Integer. Here, the argument 100 is autoboxed into an Integer. The return value is also autoboxed into an Integer.
Integer iOb = m(100);

  • In general, autoboxing and unboxing take place whenever a conversion into an object or from an object is required. This applies to expressions. Within an expression, a numeric object is automatically unboxed. The outcome of the expression is reboxed, if necessary. For ex:

Integer iOb
int i;

// The following automatically unboxes iOb, performs the increment, and then reboxes the result back into iOb.
++iOb;

// Here, iOb is unboxed, the expression is evaluated, and the result is reboxed and assigned to iOb2.
iOb = iOb + (iOb / 3);

// The same expression is evaluated, but the result is not reboxed.
i = iOb + (iOb / 3);

  • Auto-unboxing also allows you to mix different types of numeric objects in an expression. Once the values are unboxed, the standard type promotions and conversions are applied. For ex:

Integer iOb = 100;
Double dOb = 98.6;

dOb = dOb + iOb; // Results in 198.6

  • Because of auto-unboxing, we can use integer numeric objects to control a switch statement. For example, consider this fragment:

Integer iOb = 2;
switch(iOb)
{
case 1: System.out.println("one");
break;
case 2: System.out.println("two");
break;
default: System.out.println("error");
}

When the switch expression is evaluated, iOb is unboxed and its int value is obtained.

  • Java also supplies wrappers for boolean and char. These are Boolean and Character. Autoboxing/unboxing applies to these wrappers, too. For ex:

Boolean b = true;
// Below, b is auto-unboxed when used in a conditional expression, such as an if.
if(b) System.out.println("b is true");

// Autobox/unbox a char.
Character ch = 'x'; // box a char
char ch2 = ch; // unbox a char

Because of auto-unboxing, the boolean value contained within b is automatically unboxed when the conditional expression is evaluated. Thus, with the advent of autoboxing/unboxing, a Boolean object can be used to control an if statement. Because of auto-unboxing, a Boolean object can now also be used to control any of Java’s loop statements. When a Boolean is used as the conditional expression of a while, for, or do/while, it is automatically unboxed into its boolean equivalent. For example, this is now perfectly valid code:

Boolean b;
while(b) { // ...

  • Autoboxing/Unboxing can help us prevent errors. For ex:

Integer iOb = 1000; // autobox the value 1000
int i = iOb.byteValue(); // manually unbox as byte !!!
System.out.println(i); // does not display 1000 !

This displays not the expected value of 1000, but 24. The reason is that the value inside iOb is manually unboxed by calling byteValue( ), which causes the truncation of the value stored in iOb, which is 1,000. This results in the garbage value of 24 being assigned to i. Auto-unboxing prevents this type of error because the value in iOb will always auto- unbox into a value compatible with int.



Click for NEXT article.

 

Please feel free to correct me by commenting your suggestions and feedback.



 

No comments:

Post a Comment