JavaObject-oriented programmingClasses and objects

Boxing && unboxing

Wrapper classes

Each primitive type has a class dedicated to it. These classes are known as wrappers and they are immutable (just like strings). The following table list all primitive types and the corresponding wrapper classes.


The table with primitive types and the corresponding wrappers

As you can see Java provides eight wrapper classes: one for each primitive type. The third column shows the type of an argument to create an object of the corresponding wrapper class.

Boxing and unboxing

Boxing is the conversion between the primitive types and their corresponding object wrapper classes.

Unboxing is the reverse process.

int primitive = 100;

Integer reference = Integer.valueOf(primitive); // boxing

int anotherPrimitive = reference.intValue();    // unboxing

Autoboxing and auto-unboxing are the automatic conversions performed by java compiler.

double primitiveDouble = 10.8;

Double wrapperDouble = primitiveDouble; // autoboxing

double anotherPrimitiveDouble = wrapperDouble;  // auto-unboxing
Also, it's possible to create an array of Long enumerating primitive values:

Long[] array = { 1L, 2L, 3L, 4L };
There is one possible problem when unpacking. If the wrapper object is null, the unpacking throws the NullPointerException.

Long longVal = null;
long primitiveLong = longVal; // it throws NullPointerException (NPE)

Constructing wrappers based on other types

The wrapper classes have constructors for creating objects from other types. For instance, an object of a wrapper class can be created from a string (except the Character).

Integer number = new Integer("10012");   // an Integer from the string "10012"
Float f = new Float("0.01");             // a Float from the sting "0.01"
Long longNumber = new Long("100000000"); // a Long from the string "100000000"
Boolean boolVal = new Boolean("true");   // a Boolean from the string "true"

You can see the complete list of constructors in the picture above.

Also, you can create wrapper objects using special methods:

Long longVal = Long.parseLong("1000");      // a Long from the string "1000"
Long anotherLongVal = Long.valueOf("2000"); // another Long from the string "2000"

If the input string has an invalid argument (for instance, "1d0o3"), both of these methods throw the NumberFormatException.

Note, since Java9 the constructors shown above are deprecated. Use only special methods to create objects of the wrapper classes.

Primitive types vs wrapper classes

  • processing values of primitive types are faster than wrapper objects;
  • wrappers can be used in the situation if you need "null" as a no-value indicator;
  • primitive types cannot be used in collections, but wrappers can.

Comparing wrappers

As for any reference types, the operator == checks whether two objects are actually equal, i.e. whether they actually refer to the same object in memory. The method equals checks whether two objects are meaningfully equal, for example, it checks whether two wrappers or strings are having the same value.

Long i1 = Long.valueOf("2000");
Long i2 = Long.valueOf("2000");
System.out.println(i1 == i2);      // false
System.out.println(i1.equals(i2)); // true

Do not forget about this feature when working with wrappers. Although they correspond to the primitive types, they are reference types.
How did you like the theory?
Report a typo