O'Reilly logo

Java Quick Syntax Reference by Mikael Olsson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

CHAPTER 21

image

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required