16.4 Autoboxing and Auto-Unboxing

Java provides boxing and unboxing conversions that automatically convert between primitive-type values and type-wrapper objects. A boxing conversion converts a value of a primitive type to an object of the corresponding type-wrapper class. An unboxing conversion converts an object of a type-wrapper class to a value of the corresponding primitive type. These conversions are performed automatically—called autoboxing and auto-unboxing. Consider the following statements:

Integer[] integerArray = new Integer[5]; // create integerArrayintegerArray[0] = 10; // assign Integer 10 to integerArray[0]int value = integerArray[0]; // get int value of Integer

In this case, autoboxing occurs ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.