Java Wrapper Classes
Each of Java's eight primitive types has a matching wrapper class — an object type that "wraps" one primitive value. Wrappers exist because many parts of Java only work with objects: collections such as ArrayList and HashMap, generics, and anything that needs null to mean "no value".
| Primitive | Wrapper class |
|---|---|
byte |
Byte |
short |
Short |
int |
Integer |
long |
Long |
float |
Float |
double |
Double |
boolean |
Boolean |
char |
Character |
Autoboxing and unboxing
Java converts between primitives and wrappers automatically. Turning a primitive into its wrapper is called autoboxing; the reverse is unboxing:
import java.util.ArrayList;
import java.util.List;
class Main {
public static void main(String[] args) {
Integer boxed = 42; // autoboxing: int -> Integer
int plain = boxed; // unboxing: Integer -> int
System.out.println(boxed + plain);
List<Double> temps = new ArrayList<>();
temps.add(21.5); // the double is boxed automatically
temps.add(19.0);
double first = temps.get(0); // and unboxed when read
System.out.println(first + temps.get(1));
}
}84
40.5
Useful static methods and constants
The wrapper classes are also home to many helpers for parsing, converting and inspecting values:
class Main {
public static void main(String[] args) {
int n = Integer.parseInt("1234");
double d = Double.parseDouble("2.5e3");
boolean b = Boolean.parseBoolean("true");
System.out.println(n + 1);
System.out.println(d);
System.out.println(b);
System.out.println(Integer.MAX_VALUE + " / " + Integer.MIN_VALUE);
System.out.println(Integer.toBinaryString(10));
System.out.println(Integer.toHexString(255));
System.out.println(Integer.parseInt("ff", 16));
System.out.println(Integer.compare(3, 9));
System.out.println(Character.isLetter('x') + " " + Character.toUpperCase('x'));
}
}1235
2500.0
true
2147483647 / -2147483648
1010
ff
255
-1
true X
Wrapper objects also have instance methods, for example toString() and conversion methods like intValue() and doubleValue():
class Main {
public static void main(String[] args) {
Integer count = 250;
String text = count.toString();
System.out.println(text.length() + " characters");
Double price = 19.99;
System.out.println(price.intValue());
System.out.println(price.compareTo(20.0));
}
}3 characters
19
-1
Pitfall 1: comparing with ==
Wrappers are objects, so == compares references, not values. Java caches Integer objects for small values (−128 to 127), which makes == appear to work — until the numbers get bigger:
class Main {
public static void main(String[] args) {
Integer a = 100, b = 100;
Integer c = 1000, d = 1000;
System.out.println(a == b); // true - same cached object
System.out.println(c == d); // false - two different objects!
System.out.println(c.equals(d)); // true - compares values
System.out.println(c.intValue() == d.intValue());
}
}true
false
true
true
Always compare wrapper values with equals() (or unbox them first).
Pitfall 2: unboxing null
A wrapper variable can be null. Unboxing null throws a NullPointerException — a classic bug with maps, where get returns null for missing keys:
import java.util.HashMap;
import java.util.Map;
class Main {
public static void main(String[] args) {
Map<String, Integer> ages = new HashMap<>();
ages.put("Mia", 31);
try {
int age = ages.get("Tom"); // null cannot be unboxed to int
System.out.println(age);
} catch (NullPointerException e) {
System.out.println("Tom isn't in the map!");
}
int safe = ages.getOrDefault("Tom", 0);
System.out.println("Safe value: " + safe);
}
}Tom isn't in the map!
Safe value: 0
Tip: Prefer primitives for local variables and calculations — they're faster and can't be
null. Use wrappers when an API requires objects (collections, generics) or when "no value" (null) is meaningful.
Test yourself
What is the wrapper class for char?
- Char
- Character
- String
- CharWrapper
What is autoboxing?
- Converting a String to a number
- Automatic conversion of a primitive into its wrapper object
- Putting objects into an array
- Casting a double to int
How should you compare two Integer objects for equal values?
- a == b
- a.equals(b)
- a = b
- a.same(b)