Wrapper Classes in Java
Primitive values like int and double are fast and simple, but they aren't objects. They can't be stored in an ArrayList, can't be null, and have no methods. For each of the eight primitives, Java provides a wrapper class, an object that holds one primitive value: Integer for int, Double for double, and so on. Wrappers also carry useful constants and helper methods, like Integer.MAX_VALUE and Character.isDigit.
Eight Primitives, Eight Wrappers
| Primitive | Wrapper | Example |
|---|---|---|
byte | Byte | Byte.MAX_VALUE is 127 |
short | Short | |
int | Integer | Not Int |
long | Long | |
float | Float | |
double | Double | |
char | Character | Not Char |
boolean | Boolean |
All wrappers live in java.lang, are immutable and final. The six numeric wrappers extend java.lang.Number.
Why Wrappers Exist
import java.util.ArrayList;
import java.util.List;
public class Wrap {
public static void main(String[] args) {
List<Integer> marks = new ArrayList<>();
marks.add(91);
marks.add(78);
System.out.println("Marks: " + marks);
Integer pending = null;
System.out.println("Pending result: " + pending);
System.out.println(Integer.MAX_VALUE + " " + Long.MAX_VALUE);
System.out.println(Integer.toBinaryString(10)
+ " " + Integer.toHexString(255));
System.out.println(Character.isDigit('7')
+ " " + Character.toUpperCase('n'));
System.out.println(Integer.compare(3, 7) + " " + Integer.max(3, 7));
Object any = 3.14;
System.out.println(any.getClass().getSimpleName());
}
}Marks: [91, 78]
Pending result: null
2147483647 9223372036854775807
1010 ff
true N
-1 7
DoubleHow it works
- Collections need objects.
List<int>isn't allowed;List<Integer>is. Java converts91to anIntegerautomatically (autoboxing, next lesson). - Wrappers can be
null, which can mean "no value yet", such as a result that hasn't been published. - Constants:
MIN_VALUEandMAX_VALUEgive each type's range. - Helper methods: converting to binary or hex, checking characters, comparing values.
- A primitive stored in an
Objectvariable becomes its wrapper:3.14became aDouble.
Primitive or Wrapper?
Use a primitive (int) when | Use a wrapper (Integer) when |
|---|---|
| Doing arithmetic, loops, counters | Storing in collections: List<Integer>, Map<String, Double> |
| Performance matters | A value may be absent (null) |
| The value always exists | Using generics: Optional<Integer> |
| Fields and local variables in general | Working with APIs that need objects |
Common Mistakes
Writing Int or Char. The wrappers are Integer and Character.
Comparing Integer objects with ==. Use equals, or unbox to int first.
Using wrappers for every field. They cost more memory and can be null; use primitives unless you need an object.
Interview Corner
What are wrapper classes and why are they needed?
Wrapper classes are object versions of the eight primitives: Byte, Short, Integer, Long, Float, Double, Character and Boolean. They're needed because collections and generics only work with objects, because they can represent the absence of a value with null, and because they provide constants and utility methods such as Integer.parseInt and Character.isDigit. They're immutable and final.
Practice
- Easy: Print
MIN_VALUEandMAX_VALUEforByte,Short,IntegerandLong. - Harder: Count the digits, letters and spaces in
"Navika Anamaneni 2026"usingCharactermethods.
Key Takeaways
Was this tutorial helpful?