Java Wrapper Classes

Java uses primitive data types such as int and char for program performance. However, because these data values are not objects, they are passed to methods by value and cannot be passed directly by reference. Also, an int value cannot refer to an instance. Wrapper classes help create primitive data types as objects.

Primitive Type and Reference Type

Primitive type, general variable type, raw type, basic type

  • byte, short, int, long, float, double, char, boolean
  • Cannot have a null value.
  • Allocated in the stack memory area.
  • Holds the actual value.

Reference type, wrapper type

  • Byte, Short, Integer, Long, Float, Double, Character, Boolean
  • Can have a null value.
  • Because it is created with the new operator, the value is allocated in the heap memory area.
  • Holds an address value.

Primitive Type ↔ Reference Type

Boxing

  • Converting a primitive data type to a wrapper class is called boxing.
  • Primitive type → Reference type

Unboxing

  • Converting a wrapper class to a primitive data type is called unboxing.
  • Primitive type ← Reference type