Skip to content

Wrapper Classes

Estimated time to read: 1 minute

For each of the eight primitive types, there is a corresponding Java class.

Java's structure makes it impossible to use primitives in certain situations. In these cases, the wrapper classes must be used instead. Use wrappers where possible, ignore the guy, there are methods within the Wrapper Classes which are helpful!

Primitive Wrapper Class
int Integer
long Long
boolean Boolean
char Character
short Short
float Float
double Double
byte Byte

Primitive values may be assigned to the corresponding Java wrapper class.

Boxing and Unboxing

Boxing

Assigning a primitive value to an 'instance' of the wrapper class is called boxing

int i = 3; // Assigning a value of '3' to the primitive of 'i'
Integer w1 = i; // value of w1 is 3 - This is a reference to an int (integer), it isn't an int directly!
Integer w2 = 5; // value of w2 is 5 - This is a reference to an int (integer), it isn't an int directly!

Back in Java 5 and below, you would have to make an explicit object to hold the value. No longer! As the latter versions of the Java Compiler now do this for you.

Unboxing

Assigning the wrapper instance to a primitive is called unboxing

int j = w2; // Value of j is 5

The above extracts the int value from the wrapper, '5', and assigns it to 'j'