CHAPTER 21
Boxing and Unboxing
Placing a primitive variable in an object is known as boxing. This allows the primitive to be used where objects are required. For this purpose Java provides wrapper classes for each primitive – namely: Byte, Short, Integer, Long, Float, Double, Character and Boolean. An Integer object, for example, can hold a variable of the type int.
int iPrimitive = 5;Integer iWrapper = new Integer(iPrimitive); // boxing
The opposite of boxing is unboxing. This converts the object type back into its primitive type.
iPrimitive = iWrapper.intValue(); // unboxing
The wrapper classes belong to the java.lang package, which is always ...
Get Java Quick Syntax Reference 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.