Wrapper Classes
Integer, Double, Boolean, etc.
Interview Relevant: Autoboxing and unboxing questions
7 min read
What are Wrapper Classes?
Wrapper classes provide a way to use primitive data types as objects. Java provides a wrapper class for each primitive type:
| Primitive | Wrapper Class |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
Autoboxing & Unboxing
Autoboxing: Automatic conversion of primitive to wrapper class
Unboxing: Automatic conversion of wrapper class to primitive
ā ļø Null Pointer Exception: Wrapper objects can be null. Unboxing null throws NPE! Always check for null.
Why Use Wrapper Classes?
- Working with Collections (ArrayList, HashMap) which require objects
- Passing by reference when needed
- Using methods like parseInt(), parseDouble()
- Handling null values
- Type checking and conversion utilities
Code Examples
Manual boxing and unboxing of primitive types
java
1// Manual boxing (wrapping primitive in object)
2int primitiveInt = 25;
3Integer wrappedInt = Integer.valueOf(primitiveInt); // Manual boxing
4
5double primitiveDouble = 3.14;
6Double wrappedDouble = Double.valueOf(primitiveDouble);
7
8// Manual unboxing (extracting primitive from object)
9int unwrapped = wrappedInt.intValue();
10double unwrappedDouble = wrappedDouble.doubleValue();Automatic autoboxing and unboxing in Java
java
1// Autoboxing (automatic conversion)
2int value = 10;
3Integer autoBoxed = value; // Automatically wrapped
4
5// Unboxing (automatic conversion)
6Integer wrapped = Integer.valueOf(20);
7int unboxed = wrapped; // Automatically unwrapped
8
9// Works in collections
10ArrayList<Integer> numbers = new ArrayList<>();
11numbers.add(100); // Autoboxed to Integer
12int first = numbers.get(0); // Unboxed to intUseful methods provided by wrapper classes
java
1// Useful wrapper class methods
2String str = "123";
3int number = Integer.parseInt(str); // String to int
4
5int max = Integer.MAX_VALUE; // Get maximum value
6int min = Integer.MIN_VALUE; // Get minimum value
7
8String binaryStr = Integer.toBinaryString(10); // "1010"
9String hexStr = Integer.toHexString(255); // "ff"
10
11Integer parsed = Integer.parseInt("42");
12boolean result = parsed.equals(42); // trueHandling null values with wrapper classes safely
java
1// Null handling with wrapper classes
2Integer nullable = null; // Valid, primitives can't be null
3
4// NullPointerException risk!
5// int unwrapped = nullable; // Would throw NPE!
6
7// Safe approach
8if (nullable != null) {
9 int value = nullable; // Safe unboxing
10 System.out.println(value);
11}
12
13// Using Optional (Java 8+)
14Optional<Integer> optional = Optional.ofNullable(nullable);
15optional.ifPresent(System.out::println);Use Cases
- Working with Collections that require objects
- Converting Strings to numeric types
- Handling optional/nullable values
- API methods that expect wrapper types
- Accessing utility methods like parseInt(), MAX_VALUE, etc.
- Type conversion and validation
Common Mistakes to Avoid
- NullPointerException when unboxing null wrapper
- Comparing wrapper objects with == instead of .equals()
- Not understanding autoboxing performance implications
- Assuming wrapper == primitive (they're different types)
- Not caching wrapper objects (-128 to 127 for Integer)