Datasheet

}
public static void main(String args[])
{
VarArgsExample va = new VarArgsExample();
int sum=0;
sum = va.sumArrays(new int[]{1,2,3},
new int[]{4,5,6},
new int[]{10,16});
System.out.println(“The sum of the numbers is: “ + sum);
}
}
This code follows the established approach to defining and using a variable argument. The ellipsis
comes after the square brackets (that is, after the variable argument’s type). Inside the method the argu-
ment
intArrays is simply an array of arrays.
Boxing and Unboxing Conversions
One tedious aspect of the Java language in the past is the manual operation of converting primitive
types (such as
int and char) to their corresponding reference type (for example, Integer for int and
Character for char). The solution to getting rid of this constant wrapping and unwrapping is boxing
and unboxing conversions.
Boxing Conversions
A boxing conversion is an implicit operation that takes a primitive type, such as int, and automatically
places it inside an instance of its corresponding reference type (in this case,
Integer). Unboxing is the
reverse operation, taking a reference type, such as
Integer, and converting it to its primitive type, int.
Without boxing, you might add an
int primitive to a collection (which holds Object types) by doing
the following:
Integer intObject;
int intPrimitive;
ArrayList arrayList = new ArrayList();
intPrimitive = 11;
intObject = new Integer(intPrimitive);
arrayList.put(intObject); // cannot add intPrimitive directly
Although this code is straightforward, it is more verbose than necessary. With the introduction of boxing
conversions, the preceding code can be rewritten as follows:
int intPrimitive;
ArrayList arrayList = new ArrayList();
intPrimitive = 11;
// here intPrimitive is automatically wrapped in an Integer
arrayList.put(intPrimitive);
19
Chapter 1: Key Java Language Features and Libraries
05_777106 ch01.qxp 11/28/06 10:43 PM Page 19