CSC300: Boxing [13/24] Previous pageContentsNext page

Conversion from primitive type to object type is called boxing.

Conversion from object type to primitive type is called unboxing.

The primitive types we will use in this course are mostly int and double.

For int and double, the operations are as follows

Primitive type Object type Boxing (primitive to object) Unboxing (object to primitive)
int primitive = 0; Integer object = null; object = Integer.valueOf(primitive); primitive = object.intValue();
double primitive = 0.0; Double object = null; object = Double.valueOf(primitive); primitive = object.doubleValue();

Java has six additional primitive types:

Primitive type Object type Boxing (primitive to object) Unboxing (object to primitive)
boolean primitive = false; Boolean object = null; object = Boolean.valueOf(primitive); primitive = object.booleanValue();
float primitive = 0.0F; Float object = null; object = Float.valueOf(primitive); primitive = object.floatValue();
byte primitive = 0; Byte object = null; object = Byte.valueOf(primitive); primitive = object.byteValue();
char primitive = 0; Character object = null; object = Character.valueOf(primitive); primitive = object.charValue();
short primitive = 0; Short object = null; object = Short.valueOf(primitive); primitive = object.shortValue();
long primitive = 0L; Long object = null; object = Long.valueOf(primitive); primitive = object.longValue();

Previous pageContentsNext page