Wrapper Classes in Java – A Simple Guide

Published: (April 22, 2026 at 01:01 AM EDT)
2 min read
Source: Dev.to

Source: Dev.to

What is a Wrapper Class?

A wrapper class converts a primitive data type into an object, “wrapping” the primitive value inside an object.

PrimitiveWrapper
intInteger
charCharacter
doubleDouble
booleanBoolean

Why Do We Need Wrapper Classes?

Collections work only with objects

Java collections such as ArrayList cannot store primitive types.

// Not allowed
ArrayList list;

// Allowed
ArrayList list;

Utility Methods

Wrapper classes provide useful conversion methods.

int i = Integer.parseInt("123");        // String → int
Double d = Double.valueOf("10.5");     // String → Double

Object‑Oriented Features

APIs, frameworks, and generics often require objects rather than primitives. Wrapper classes enable you to work within an object‑oriented paradigm.

Autoboxing and Unboxing

Java automatically converts between primitives and their wrapper objects:

  • Autoboxing – primitive → object
  • Unboxing – object → primitive

This automatic conversion makes code cleaner and reduces boilerplate.

Where Are Wrapper Objects Stored?

  • Primitive values – stored on the stack.
  • Wrapper objects – stored on the heap.
  • Reference variables – stored on the stack, pointing to heap objects.
Integer a = 10; // 'a' reference is on the stack; the Integer object is on the heap.

Integer Caching

Java caches Integer objects for values between ‑128 and 127.

Integer x = 100;
Integer y = 100;
System.out.println(x == y); // true (same cached object)

Values outside this range create distinct objects:

Integer x = 200;
Integer y = 200;
System.out.println(x == y); // false (different objects)

Final Thoughts

Wrapper classes are essential in Java. While primitives offer speed, wrappers provide the flexibility needed for collections, APIs, and object‑based operations. Mastering wrapper classes, autoboxing, and their memory behavior is crucial, especially for interview preparation.

0 views
Back to Blog

Related posts

Read more »