Wrapper Classes
Overview
Java's Collections Framework (ArrayList, HashMap, HashSet) is built entirely using Generics. Generics require types to be Objects, not primitives. You cannot write ArrayList<int>; you must write ArrayList<Integer>. This is where Wrapper Classes come in.
Every one of Java's 8 primitive types has a corresponding Wrapper Class that boxes the primitive inside an Object:
- byte → Byte
- short → Short
- int → Integer
- long → Long
- float → Float
- double → Double
- char → Character
- boolean → Boolean
Beyond enabling collections, Wrapper Classes provide extremely useful utility methods. Integer.parseInt("42") converts a String to an int. Integer.MAX_VALUE gives the largest possible int value (2,147,483,647). Integer.toBinaryString(255) converts a number to its binary representation.
Syntax
public class Main {
public static void main(String[] args) {
// 1. Wrapping a primitive into an Object
Integer myInt = Integer.valueOf(42);
Double myDouble = Double.valueOf(3.14);
Boolean myBool = Boolean.valueOf(true);
// 2. Parsing Strings to primitives (Most common use!)
int parsed = Integer.parseInt("100");
double pi = Double.parseDouble("3.14159");
boolean flag = Boolean.parseBoolean("true"); // case-insensitive
// 3. Useful Constants
System.out.println(Integer.MAX_VALUE); // 2,147,483,647
System.out.println(Integer.MIN_VALUE); // -2,147,483,648
System.out.println(Double.MAX_VALUE);
// 4. Number Base Conversions
System.out.println(Integer.toBinaryString(255)); // "11111111"
System.out.println(Integer.toHexString(255)); // "ff"
System.out.println(Integer.toOctalString(255)); // "377"
// 5. Comparing Wrapper objects correctly
Integer a = 200;
Integer b = 200;
System.out.println(a == b); // false! (different objects)
System.out.println(a.equals(b)); // true (same value)
}
}Common Pitfalls
Using
==to compare Integer objects outside the cache range. Java cachesIntegerobjects for values -128 to 127. For values in this range,Integer a = 100; Integer b = 100; a == breturnstruebecause they point to the same cached object. For values outside this range (like 200), it returnsfalsebecause new objects are created. Always use.equals()for object comparison.Unboxing a null Wrapper class. If an
Integervariable isnulland you try to use it in arithmetic (int x = nullableInteger + 1), Java attempts to unbox the null to anint, causing aNullPointerException. Always null-check Wrapper class variables before arithmetic.Ignoring the performance overhead of boxing/unboxing in tight loops. If you have a loop running millions of times and you're doing
Integer sum = 0; sum += i;, the constant boxing and unboxing creates millions of temporary objects. Always use primitives inside performance-critical loops.
Interview Questions
Why can't primitives be used directly as generic type parameters (e.g., ArrayList<int>)?
Java Generics are implemented through type erasure, which the compiler uses at the bytecode level to work with the Object class. Primitives are not Objects; they are raw value types stored on the Stack. Therefore, Generics require Object types, and Wrapper classes bridge this gap.
What is the Integer Cache in Java and why does it matter?
To optimize memory, Java pre-creates and caches Integer objects for all values from -128 to 127 at JVM startup. When you autobox an int in this range, you get a cached object, so Integer a = 100; Integer b = 100; a == b evaluates to true. Outside this range, new objects are created, and == returns false. This is why equals() must always be used for Integer comparison.
Real-World Example
JSON parsing libraries (like Gson or Jackson) use Wrapper types instead of primitives for object fields because Wrapper types can represent null — indicating a missing JSON field. A primitive int cannot be null, but Integer age can, accurately reflecting optional JSON data.
// With Integer (nullable), we can detect missing JSON fields
public class UserProfile {
String name;
Integer age; // Can be null if "age" key missing from JSON
Boolean active; // Can be null if "active" key missing from JSON
}
// If 'age' is missing from JSON, Jackson sets it to null
// A primitive 'int age' would default to 0 — wrong meaning!Check Your Knowledge
Test your understanding of Wrapper Classes with these quick questions.