Nearby lessons

48 of 125

Java - Wrapper Classes

📌 What You Will Learn
  • 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).

PrimitiveWrapper class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Converting Primitive to Object (Manually)

The old way (before Java 5) was to use the wrapper constructor or the valueOf method:

Example02
Loading editor…

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.
In simple words: Autoboxing and unboxing are automatic conversions done by the compiler. Integer i = 10; really writes Integer.valueOf(10), and int a = i; really writes i.intValue().
Example03
Loading editor…

Important Methods of Wrapper Classes

Wrapper classes give many useful static methods. Memorise these five families:

MethodPurposeExample
valueOf(String)Convert a String into a wrapper objectInteger.valueOf("100")
parseXxx(String)Convert a String into a primitiveInteger.parseInt("100") -> int 100
toString(xxx)Convert a primitive into StringInteger.toString(100) -> "100"
xxxValue()Convert wrapper to primitivei.intValue(), d.doubleValue()
MIN_VALUE / MAX_VALUERange constants of each typeInteger.MAX_VALUE
Trainer's Note: Classic interview question: what is the difference between parseInt and valueOf? parseInt("100") returns an int (primitive). valueOf("100") returns an Integer (object). One gives the gift, the other gives the gift in a box.
In simple words: `parseInt` returns a primitive `int`; `valueOf` returns an `Integer` object. Use parseInt when you need a number to calculate with, and valueOf when you need an object for a collection.
Example04
Loading editor…

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.

Trainer's Note: Golden rule for beginners: always use `.equals()` to compare wrapper objects or Strings, never ==. Use == only for primitives.
In simple words: Always compare wrapper objects with `.equals()`, never with `==`. == checks whether two references point to the same box; .equals() checks whether the values inside are equal.
Example05
Loading editor…

Wrapper Methods Used Most in Real Code

Example06
Loading editor…

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:

ConversionMethodExample
Primitive -> ObjectvalueOf() or constructorInteger.valueOf(10)
Object -> PrimitivexxxValue()i.intValue()
String -> ObjectvalueOf(String)Integer.valueOf("10")
Object -> StringtoString()i.toString()
Primitive -> StringString.valueOf() or toString()String.valueOf(10)
String -> PrimitiveparseXxx()Integer.parseInt("10")
Trainer's Note: The single toString() method appears twice in this table — Java uses one method name for both 'Object to String' and 'primitive to String'. That is why remembering these six directions makes every wrapper question easy.
Example07
Loading editor…
📝 Key Takeaways
  • 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.

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.