Wrapper Classes & Autoboxing
Wrapper Classes & Autoboxing
Java's primitive types — int, double, boolean, and the rest — are fast and memory-efficient, but they are not objects. That becomes a problem the moment you need to store them in a collection, pass them to a method that expects Object, or call a utility method on a number. Wrapper classes solve this by wrapping each primitive in a full object.
The Eight Wrapper Classes
Every primitive has a corresponding class in java.lang:
byte→Byteshort→Shortint→Integerlong→Longfloat→Floatdouble→Doublechar→Characterboolean→Boolean
You will work with Integer, Double, and Boolean most often. They are the ones you encounter every day in real Java code.
Autoboxing and Unboxing
Before Java 5 you had to convert manually. Modern Java does it for you automatically:
- Autoboxing — the compiler converts a primitive to its wrapper object automatically.
- Unboxing — the compiler converts a wrapper object back to its primitive automatically.
The conversion is invisible at the source level, but the compiler inserts the calls. Understanding this matters for performance and for a subtle bug you will see shortly.
Useful Methods on Wrapper Classes
Wrapper classes are not just containers — they carry static utility methods that primitives cannot have.
Parsing Strings into Primitives
One of the most common uses of wrapper classes is parsing user input or configuration values stored as text into usable numbers or booleans.
Integer.parseInt("42") returns an int primitive. Integer.valueOf("42") returns an Integer object (using the cache described below). Prefer parseInt when you need a primitive; prefer valueOf when you need an object.
The Integer Cache Pitfall
Here is one of the most famous beginner traps in Java. The JVM caches Integer objects for values between -128 and 127 inclusive. When you autobox a number in that range, you get back the same cached object every time. Outside that range a new object is created each time.
== operator checks reference identity (are these the same object in memory?), not value equality. Two Integer objects holding 200 are different objects and == returns false. Always use .equals() to compare wrapper values.
Null and NullPointerException During Unboxing
Because wrapper objects are real objects, they can be null. Unboxing a null throws a NullPointerException — a common source of crashes when working with collections or method return values.
value.intValue(). Calling any method on null throws NullPointerException. The compiler cannot warn you because the assignment looks legal — always guard wrapper references that might be null.
Summary
Wrapper classes bridge primitives and the object world. Autoboxing and unboxing let you write natural-looking code while the compiler handles conversions. Keep three rules in mind: use parseInt / parseDouble to convert strings to values; always compare wrappers with .equals(), never ==; and guard against null before unboxing. With these habits you will avoid the most common wrapper-related bugs.