Data Type - Boxing (Autoboxing|Autowrapping) and Primitive Wrapper

Card Puncher Data Processing


Boxing and Unboxing are the terms used to describe automatically wrapping a primitive type in a wrapper class and unwrapping it as necessary.

This operation has the advantage to add basic method functions (such as toUpperCase, length, …).

The wrapper classes for primitive types have generally the first letter as uppercase to differentiate them from the primitive one. For instance, String for string.

The code will continue to work - no matter if it's a primitive type or wrapper class thanks to autoboxing.

The term autowrapping would be more consistent, but the “boxing” metaphor was taken from C#.

Boxing and unboxing is a courtesy of the compiler. The compiler inserts the necessary calls when it generates the instructions.


Ex with Java, the following class are the wrappers:

Integer i = 10;
// Is like saying:
Integer i = Integer.valueOf(10);
String foo = "nico";
// Is like saying:
String foo = new String("nico");

Primitive types vs wrapper

Use the primitive types where there is no need for objects for the following reasons.

  • Primitive types may be a lot faster than the corresponding wrapper types, and are never slower.
  • The immutability (can't be changed after creation) of the wrapper types may make it their use impossible.
  • There can be some unexepected behavior involving == (compare references) and .equals() (compare values).

If we are autoboxing some value to one of the wrapper classes (Boolean, Byte, Short, Char, Integer, Long), the compiler sometimes creates a new object and sometimes uses the pool of values, similarly like with Strings.

Here are some rules as with Java 5 :

  • autoboxing to Boolean and Byte always returns an object from the pool
  • autoboxing to Char, Short, Integer and Long returns an object from the pool when the autoboxed value is between -128 and 127 (inclusive)
  • autoboxing of Float and Double does not use the pool and always returns a new object


Generally, the wrapper classes are:

  • immutable: the information contained inside the wrapper can't change after the wrapper has been constructed.
  • final, so you cannot subclass them.

Documentation / Reference

Recommended Pages
Java Conceptuel Diagram
Java - (Class|Object|Member) (Initialization|Instantiation)

Class (Initialization|Instantiation) in java. During an class initialization: Java's class loader loads the main method Java's byte code verifier verifies the class. The first initialization...
Java Conceptuel Diagram
Java - (Primitive|Native) Data Type

Java has a fixed set of primitive types: boolean, byte, short, int, long, char, float, and double. They can also be used in a wrapper class in order to enhance the functionalities....
Java Conceptuel Diagram
Java - Character (char)

character in java. There is two type: char which is a primitive type java/lang/CharacterCharacter which is a primitive wrapper around a char primitive and adds functionalities such as the possibility...
Java Conceptuel Diagram
Java - Generic Class

generic class All instances of a generic class have the same run-time class, regardless of their actual type parameters. A class generic has the same behavior for all of its possible type parameters;...
Java Conceptuel Diagram
Java - Integer

integer data type in Java. int (or Integer) is the 32 bit implementation of an integer. Java can also store an integer on 64 bit with a long. An integer in java is a sub-class of number They are...
Java Conceptuel Diagram
Java - long - Long (integer 64 bit)

In Java, the long data type stores integer on 64 bit while the integer data type stores integer on 32bit. The primitive wrapper java/lang/LongLong is a subclass of java/lang/NumberNumber in java ...
Javascript - Primitive Data Type

Javascript has 6 primitive data type. string number boolean null undefined symbol Except for null and undefined, all primitive values have object equivalents that wrap around the primitive...

Share this page:
Follow us:
Task Runner