Nearby lessons
48 of 125Java - Wrapper Classes
- Why wrapper classes exist
- How to convert primitive to object and object to primitive
- Autoboxing and unboxing (automatic conversion)
- Important methods: valueOf, parseXxx, toString, xxxValue
- The famous Integer caching (range -128 to 127)
Wrapper Classes is a core concept of the Java language. This lesson explains Why Do We Need Wrapper Classes?, Converting Primitive to Object (Manually) and Autoboxing and Unboxing (Java 5 onwards) with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
Why Do We Need Wrapper Classes?
Primitive types (int, char, boolean, etc.) are not objects. But many parts of Java work only with objects. For example, Collections (ArrayList, HashMap) can store only objects — they cannot store a raw int.
To solve this, Java provides wrapper classes — one class for each primitive type, which wraps the primitive value inside an object. Think of a wrapper class as a gift box: the primitive value (the gift) is inside the box (the object).
| Primitive | Wrapper class |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
Converting Primitive to Object (Manually)
The old way (before Java 5) was to use the wrapper constructor or the valueOf method:
Autoboxing and Unboxing (Java 5 onwards)
From Java 5, this conversion became automatic:
- Autoboxing — primitive to wrapper object, done automatically.
- Unboxing — wrapper object to primitive, done automatically.
Important Methods of Wrapper Classes
Wrapper classes give many useful static methods. Memorise these five families:
| Method | Purpose | Example |
|---|---|---|
| valueOf(String) | Convert a String into a wrapper object | Integer.valueOf("100") |
| parseXxx(String) | Convert a String into a primitive | Integer.parseInt("100") -> int 100 |
| toString(xxx) | Convert a primitive into String | Integer.toString(100) -> "100" |
| xxxValue() | Convert wrapper to primitive | i.intValue(), d.doubleValue() |
| MIN_VALUE / MAX_VALUE | Range constants of each type | Integer.MAX_VALUE |
equals() vs == (Important for Wrappers)
With wrapper objects, == compares references (are they the same box?) while equals() compares values (is the gift inside same?). This leads to a famous surprise called Integer caching:
Why? Java caches Integer objects for the range -128 to +127. For these values it reuses the same object, so == is true. Outside this range, new objects are created each time, so == is false even though the values are equal.
Wrapper Methods Used Most in Real Code
The Six Conversions Between Primitive, Object and String
The classic material teaches all the conversion directions between the three worlds: primitive, wrapper object, and String. Master these six:
| Conversion | Method | Example |
|---|---|---|
| Primitive -> Object | valueOf() or constructor | Integer.valueOf(10) |
| Object -> Primitive | xxxValue() | i.intValue() |
| String -> Object | valueOf(String) | Integer.valueOf("10") |
| Object -> String | toString() | i.toString() |
| Primitive -> String | String.valueOf() or toString() | String.valueOf(10) |
| String -> Primitive | parseXxx() | Integer.parseInt("10") |
- Wrapper classes box each primitive into an object: int->Integer, char->Character, etc.
- Autoboxing converts primitive to wrapper automatically; unboxing does the reverse.
- parseInt returns primitive int; valueOf returns Integer object.
- Use equals() not == for wrapper objects and Strings.
- Integer values from -128 to 127 are cached, so == can be true for small values.
- Collections can store only objects, so wrappers are essential there.