Learn Java
import com.learnjava.variablesdatatypes.ReferenceWrapperTypes;

Reference Types & Wrapper Classes in Java

@Concept

Anything that isn't one of the eight primitives is a reference type — objects, arrays, and String all qualify — and a reference-type variable holds a reference to an object living elsewhere in memory, not the object's data directly. Every primitive has a matching wrapper class — int with Integer, double with Double, boolean with Boolean, and so on — that packages that primitive value inside a real object, which matters anywhere the language requires an object instead of a raw primitive, such as a generic collection like a list of Integer, which cannot hold a raw int at all.

Autoboxing and unboxing let Java convert between a primitive and its wrapper automatically in most contexts, but it's a real conversion happening behind the scenes, not a free one. Wrapper objects can also be null, while primitives never can — unboxing a null wrapper throws a NullPointerException at runtime.

@HowItWorks
1

What counts as a reference type

Objects, arrays, and String — anything that isn't one of the eight primitives.

2

What a reference actually holds

The variable stores a reference to the object, not the object's bytes directly.

3

Every primitive has a wrapper class

int pairs with Integer, double with Double, char with Character, boolean with Boolean, and so on, each wrapping the primitive value inside a real object.

4

Autoboxing and unboxing

Java automatically converts a primitive into its wrapper (boxing) and back again (unboxing), most visibly when a primitive needs to go into a generic collection.

5

Wrappers can be null, primitives cannot

An uninitialized Integer field defaults to null, and unboxing a null wrapper throws a NullPointerException.

@Example
import java.util.ArrayList;
 
public class WrapperDemo {
public static void main(String[] args) {
ArrayList<Integer> scores = new ArrayList<>();
scores.add(90);
int first = scores.get(0);
System.out.println("Stored: " + scores);
System.out.println("Unboxed: " + first);
}
}
Output
Stored: [90]
Unboxed: 90
1A list of Integer can only hold objects, so it needs Integer rather than the primitive int.
2scores.add(90) autoboxes the primitive 90 into an Integer object before storing it.
3scores.get(0) returns an Integer, and assigning it to the primitive int first triggers automatic unboxing.
4Both println calls run once each, confirming the value survived the round trip through boxing and unboxing unchanged.
@UseCases
Storing numbers inside a generic collection like a list of Integer or a map of String to Double
Debugging a NullPointerException thrown by unboxing a wrapper field that was never set
Understanding why comparing two Integer objects with == can behave unexpectedly for larger values
Choosing a primitive over a wrapper inside a performance-sensitive loop to avoid boxing overhead

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.