Java Wrapper Classes

Wrapper classes turn Java's primitive types into full objects. Because collections like ArrayList and HashMap can only hold objects, wrappers such as Integer and Double are what let you store numbers in them. This lesson explains why wrappers exist and how autoboxing hides the conversion for you.

Java has two families of types. Primitives like int, double, char, and boolean are fast and simple but are not objects, so they have no methods and cannot be stored in collections. Wrapper classes are object versions of each primitive. Every primitive has a matching wrapper, and the wrapper adds useful methods and lets the value be treated as an object.

PrimitiveWrapper class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Notice that most wrapper names are just the capitalised primitive, but int becomes Integer and char becomes Character. These small differences trip up beginners, so it is worth memorising the two odd ones.

Autoboxing and unboxing

In modern Java you rarely convert between a primitive and its wrapper by hand. The compiler does it automatically. Turning a primitive into a wrapper is called autoboxing, and turning a wrapper back into a primitive is called unboxing. This is why you can write list.add(5) even though the list holds Integer objects.

Autoboxing and unboxing in action

public class Main {
  public static void main(String[] args) {
    Integer boxed = 42;    // autoboxing: int 42 becomes Integer
    int unboxed = boxed;   // unboxing: Integer becomes int

    System.out.println(boxed + unboxed); // 84

    // Wrapper methods that a plain int does not have
    System.out.println(Integer.parseInt("100") + 1); // 101
    System.out.println(Integer.MAX_VALUE);           // 2147483647
  }
}

Wrapper classes also carry handy static tools. Integer.parseInt turns a String into an int, which is essential when reading numbers typed by a user. Constants like Integer.MAX_VALUE tell you the largest value the type can hold.

Why collections need wrappers

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    // ArrayList<int> is NOT allowed; use the wrapper
    ArrayList<Integer> scores = new ArrayList<>();
    scores.add(90);  // int 90 is autoboxed to Integer
    scores.add(75);

    int first = scores.get(0); // auto-unboxed back to int
    System.out.println("First score: " + first);
  }
}
Note: Compare wrapper objects with .equals(), not ==. The == operator compares object identity, and while Java caches small Integer values (-128 to 127), larger ones create separate objects, so == can surprisingly return false for equal values like 1000.
Note: A wrapper can be null, but a primitive cannot. If you unbox a null Integer into an int, Java throws a NullPointerException. Be careful when a HashMap.get returns null and you assign it to a primitive.
  • Each primitive has a matching wrapper class; int to Integer, char to Character.
  • Wrappers are objects, so they work with collections and have useful methods.
  • Autoboxing and unboxing convert between primitives and wrappers automatically.
  • Use .equals() to compare wrappers, and watch out for null unboxing.

Exercise: Java Wrapper Classes

What is autoboxing in Java?