Wrapper Classes in Java — What and Why
Fast recall
Quick revision
Core answer
Integer, Double, Character, and Boolean represent primitive values as objects. They are needed where reference types are required, especially generics and collections, and they provide parsing, comparison, constants, and conversion helpers. Wrappers can be null; unboxing null throws NullPointerException. Their object layout and memory cost are JVM details.Whether you understand the object-vs-primitive gap in Java and can name real use cases where wrapper classes are required.
Thinking wrapper classes are just Integer version of int. They also serve as a namespace for utility methods and constants that can't exist on primitives.
Mention that wrapper classes are immutable and final — just like String. And that null is a valid value for Integer but not for int — which means Integer fields can represent 'no value present', which primitives cannot.
Interview-ready guide
Interview answer
Java draws a hard line between primitives and objects, and wrapper classes exist to cross it. An int isn't an object, but the entire collections framework and generics only work with objects — you simply can't write ArrayList, you have to write ArrayList. So every primitive gets a matching wrapper: int to Integer, double to Double, eight in total.
But I'd push back on the idea that a wrapper is just 'the object version of int.' It's also the home for utility methods and constants that have nowhere else to live — Integer.parseInt(), Integer.MAX_VALUE, Character.isDigit(). None of those could hang off a bare primitive, because a primitive is a value, not a type with methods.
Two details I'd make sure to mention. First, wrappers are immutable and final, exactly like String — once an Integer holds a value, it never changes. Second, an Integer can be null while an int can't, which makes it the right choice for an optional or absent value, like a database column that might be missing. That same nullability is the trap, though: unboxing a null Integer throws a NullPointerException. So my rule is to default to `int`, and reach for Integer only when you actually need null or generics.
Wrapper Classes in Java — What and Why
Direct Answer
Wrapper classes such as Integer, Double, Character, and Boolean represent primitive values as objects. They are needed where reference types are required, especially generics and collections, and they provide parsing, comparison, constants, and conversion helpers. Wrappers can be null; unboxing null throws NullPointerException. Their object layout and memory cost are JVM details.
Code Example
import java.util.ArrayList;
import java.util.HashMap;
public class WrapperDemo {
public static void main(String[] args) {
// Collections require objects — wrappers enable this
ArrayList<Integer> scores = new ArrayList<>();
scores.add(95); // autoboxed: Integer.valueOf(95)
scores.add(72);
int first = scores.get(0); // unboxed: .intValue()
// Utility methods — only available via wrapper class
int parsed = Integer.parseInt("42"); // String to int
String bin = Integer.toBinaryString(255); // "11111111"
System.out.println(bin);
// Constants
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MIN_VALUE); // -2147483648
// Character utilities — very useful in string problems
char c = '5';
System.out.println(Character.isDigit(c)); // true
System.out.println(Character.isLetter(c)); // false
System.out.println(Character.toUpperCase('a')); // A
// Nullable numeric values — Integer can be null, int cannot
Integer optionalAge = null; // represents 'age not provided'
if (optionalAge != null) {
System.out.println("Age: " + optionalAge);
} else {
System.out.println("Age not provided");
}
}
}
Common Mistakes & Pitfalls
- Thinking a wrapper is just the
Integerversion ofint— it is also the namespace for utility methods (parseInt(),toBinaryString()) and constants (Integer.MAX_VALUE) that simply cannot exist on a primitive. - Forgetting wrappers are immutable — like
String, anIntegernever changes; doingcount++on anIntegersilently creates a brand-new object viavalueOf()rather than editing in place. - Unboxing a
nullwrapper —Integer x = null; int y = x;compiles but throwsNullPointerExceptionat runtime, because the hiddenx.intValue()runs onnull. - Comparing wrappers with
==—==compares object references, not values; use.equals()or unbox to a primitive first. - Defaulting to
Integerout of habit — it uses a wrapper object and can benull; prefer plainintfor counters and loop variables, and reach for the wrapper only when you genuinely neednullor generics.
Check your understanding
Answer these without looking back at the article.
- What is autoboxing and how do wrapper classes enable it?
- Why are wrapper classes immutable in Java?
- What is the difference between Integer.parseInt() and Integer.valueOf()?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.