DevNotes
Support
Core Java · Chapter 08

Wrapper Classes & Autoboxing

Object versions of primitives, autoboxing and unboxing, Integer caching and the null-unboxing trap.

◷ 6 min read§ 24 sections? 10 interview questions{ } 26 code examples

#Introduction

Java provides Wrapper Classes to represent primitive data types as objects. They are heavily used in:

  • Collections (ArrayList, HashMap, HashSet)
  • Generics
  • Streams API
  • Reflection
  • Frameworks like Spring Boot and Hibernate

Without wrapper classes, many Java APIs would not work with primitive values.

#What are Wrapper Classes?

A Wrapper Class is an object representation of a primitive data type.

PrimitiveWrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean
Java
int age = 25;
Integer wrapperAge = Integer.valueOf(age);

Now wrapperAge is an object.

#Why Do We Need Wrapper Classes?

Collections store objects, not primitives.

Wrong:

Java
ArrayList<int> list = new ArrayList<>();   // Compilation Error

Correct:

Java
ArrayList<Integer> list = new ArrayList<>();

Collections require object references.

#Wrapper Class Hierarchy

  • Object
    • Number
      • Byte
      • Short
      • Integer
      • Long
      • Float
      • Double
    • Character
    • Boolean

#Creating Wrapper Objects

Java
Integer number = Integer.valueOf(100);

Method 2 — Autoboxing

Java
Integer number = 100;

The compiler converts it internally to:

Java
Integer number = Integer.valueOf(100);

Deprecated Constructor

Java
Integer number = new Integer(100);

#Unboxing

Converting Wrapper → Primitive.

Java
Integer number = 100;
int value = number;

The compiler automatically converts this to:

Java
int value = number.intValue();

#Autoboxing

Introduced in Java 5. Primitive → Wrapper.

Java
Integer num = 50;

Internally:

Java
Integer num = Integer.valueOf(50);

No explicit conversion required.

#Auto-Unboxing

Wrapper → Primitive.

Java
Integer number = 10;
int value = number;

Internally:

Java
int value = number.intValue();

#Complete Example

Java
Integer age = 25;
int x = age;
Integer y = x;
Primitive
Wrapper
Primitive

#Integer Caching

This is one of the most common interview questions.

Java
Integer a = 100;
Integer b = 100;
System.out.println(a == b);
Output
true

Why? Java caches Integer objects from -128 to 127. Both variables refer to the same cached object.

Java
Integer a = 200;
Integer b = 200;
System.out.println(a == b);
Output
false

Because 200 is outside the cache range, two separate objects are created.

Integer Cache Memory

-128
…
0
…
100
…
127
Objects within this range are reused

#equals() vs ==

Java
Integer a = 200;
Integer b = 200;
System.out.println(a == b);
Output
false
Java
System.out.println(a.equals(b));
Output
true

#NullPointerException During Unboxing

Java
Integer number = null;
int value = number;
Output
NullPointerException

Because Java tries to execute number.intValue() on null.

#Useful Wrapper Methods

MethodCodeOutput
ParseInteger.parseInt("100")100 (int)
valueOf()Integer.valueOf("200")200 (Integer object)
toString()Integer.toString(10)"10"
compare()Integer.compare(20, 30)-1
max()Integer.max(10, 20)20
min()Integer.min(10, 20)10
sum()Integer.sum(20, 30)50

#Parsing Numbers

Java
Double.parseDouble("10.25");
Long.parseLong("500");
Float.parseFloat("20.5");
Boolean.parseBoolean("true");

Used frequently while reading configuration files, JSON, CSV and properties files.

#Wrapper Classes in Collections

Java
List<Integer> numbers = List.of(10, 20, 30);
10
Integer
Stored in List
Autoboxing happens automatically

#Wrapper Classes in Streams

Java
List<Integer> list = List.of(1, 2, 3);

list.stream()
    .map(i -> i * 2)
    .forEach(System.out::println);

Wrappers are required because collections cannot store primitives.

Primitive Streams

Instead of Stream<Integer>, Java provides:

  • IntStream
  • LongStream
  • DoubleStream

These avoid boxing overhead and improve performance.

#Real Project Examples

Database

Java
Integer employeeId;

Allows null values from databases. A primitive int cannot represent SQL NULL.

DTO

Java
private Integer age;

A missing JSON field becomes null.

Collections

Java
List<Integer>
Set<Long>
Map<Integer, String>

Collections require wrapper classes.

#Wrapper Classes are Immutable

Java
Integer x = 100;
x++;
Old Integer100
New Integer101
A new object is created: wrapper classes are immutable

#Interview Questions

Q1Why do wrapper classes exist?

To represent primitive values as objects so they can be used with collections, generics, and frameworks.

Q2What is Autoboxing?

Automatic conversion from primitive to wrapper object.

Q3What is Auto-Unboxing?

Automatic conversion from wrapper object to primitive.

Q4What is Integer caching?

Java caches Integer objects from -128 to 127.

Q5Why does Integer a = 100; Integer b = 100; return true with ==?

Because both references point to the same cached object.

Q6Why does Integer a = 200; Integer b = 200; return false with ==?

Because separate objects are created outside the cache range.

Q7Can Auto-Unboxing throw NullPointerException?

Yes — when attempting to unbox a null wrapper object.

Q8Which wrapper classes extend Number?

Byte, Short, Integer, Long, Float, and Double.

Q9Are wrapper classes mutable?

No. All wrapper classes are immutable.

Q10When should we use primitive types instead of wrappers?

Use primitives for better performance when null values are not required. Use wrappers when working with collections, generics, databases, serialization, or nullable fields.

#Common Mistakes

  • Using == for wrapper value comparison.
  • Forgetting Integer cache behaviour.
  • Unboxing null values.
  • Using deprecated wrapper constructors.

#Best Practices

  • Use wrapper classes with collections and generics.
  • Prefer primitives in performance-critical code when null is unnecessary.
  • Use equals() to compare wrapper values.
  • Always check for null before unboxing.
  • Use parseXxx() methods for converting strings to primitives.

#Quick Revision

Primitive
Wrapper
Collections
Primitive
Integer Cache
-128 to 127
Shared Objects

#Interview Cheat Sheet

RequirementUse
High performancePrimitive
CollectionsWrapper
Nullable valueWrapper
Generic typeWrapper
Compare wrapper valuesequals()
Parse StringparseXxx()

#Chapter Summary

After completing this chapter, you should be able to:

  • Explain the purpose of wrapper classes.
  • Understand autoboxing and auto-unboxing.
  • Describe Integer caching and its effect on ==.
  • Avoid common pitfalls such as NullPointerException during unboxing.
  • Choose between primitives and wrapper classes based on performance and application requirements.
  • Confidently answer wrapper class interview questions for Java developers.