Wrapper Classes with Auto boxing and Unboxing

Java 9 min min read Updated: Mar 31, 2026 Beginner
Wrapper Classes with Auto boxing and Unboxing
Beginner Topic 8 of 14

Wrapper Classes with Auto Boxing and Unboxing

In Java, primitive data types such as int, double, char, and boolean are very efficient for storing simple values. However, many Java APIs, collections, and framework-level features work with objects rather than primitives. To bridge this gap, Java provides wrapper classes.

Wrapper classes allow primitive values to be treated as objects. Along with this, Java provides autoboxing and unboxing, which automatically convert between primitives and wrapper objects.

Key Concept: Wrapper classes convert primitive values into objects, and autoboxing/unboxing automatically handle conversion between primitive types and their corresponding wrapper objects.

What are Wrapper Classes?

Wrapper classes are predefined classes in Java that wrap primitive data types into objects.

Java provides one wrapper class for each primitive type:

Primitive Type Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Why Wrapper Classes are Needed

Wrapper classes are needed because many Java features require objects, not primitive values.

Common situations:

  • collections like ArrayList store objects, not primitives
  • utility methods often work with objects
  • frameworks, generics, and APIs usually expect objects
  • primitive values sometimes need methods and conversion utilities

Primitive vs Wrapper Example

Primitive values are not objects, so they cannot be used directly where objects are required.

Invalid Example

java // ArrayList list = new ArrayList(); // Invalid

Collections require wrapper objects:

Valid Example

java import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add(10); list.add(20); System.out.println(list); } }

Here, Integer is used instead of int.

Creating Wrapper Objects

Wrapper objects can be created in different ways.

Using Constructor Style

java Integer i = new Integer(10); Double d = new Double(25.5);

In modern Java, direct factory methods or autoboxing are more common.

Using valueOf()

java Integer i = Integer.valueOf(10); Double d = Double.valueOf(25.5);

Using Autoboxing

java Integer i = 10; Double d = 25.5;

Autoboxing

Autoboxing is the automatic conversion of a primitive value into its corresponding wrapper object.

Example

java int x = 100; Integer obj = x; // Autoboxing System.out.println(obj);

Here, Java automatically converts int into Integer.

Unboxing

Unboxing is the automatic conversion of a wrapper object into its corresponding primitive value.

Example

java Integer obj = 200; int x = obj; // Unboxing System.out.println(x);

Here, Java automatically converts Integer into int.

Auto Boxing and Unboxing Together

java public class Main { public static void main(String[] args) { int a = 10; Integer b = a; // autoboxing Integer c = 20; int d = c; // unboxing System.out.println(b); System.out.println(d); } }

Wrapper Classes in Collections

One of the biggest uses of wrapper classes is with collections.

java import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList numbers = new ArrayList(); numbers.add(10); // autoboxing numbers.add(20); numbers.add(30); int value = numbers.get(0); // unboxing System.out.println(value); } }

Here:

  • 10 becomes Integer automatically
  • retrieved Integer becomes int automatically

Common Methods of Wrapper Classes

Wrapper classes provide many useful utility methods.

  • valueOf()
  • parseInt(), parseDouble()
  • toString()
  • compareTo()
  • equals()
  • MAX_VALUE, MIN_VALUE

valueOf() Method

Converts a primitive or string into a wrapper object.

java Integer i = Integer.valueOf(100); Integer j = Integer.valueOf("200"); System.out.println(i); System.out.println(j);

parseInt() and parseDouble()

These methods convert strings into primitive values.

java int x = Integer.parseInt("123"); double y = Double.parseDouble("45.67"); System.out.println(x); System.out.println(y);

These are very useful when reading numeric input from text sources.

toString() Method

Converts a primitive or wrapper value into string form.

java String s = Integer.toString(100); System.out.println(s);

MAX_VALUE and MIN_VALUE

Wrapper classes provide constants representing the maximum and minimum limits of primitive types.

java System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MIN_VALUE); System.out.println(Double.MAX_VALUE);

compareTo() Method

Compares two wrapper objects.

java Integer a = 10; Integer b = 20; System.out.println(a.compareTo(b)); System.out.println(b.compareTo(a)); System.out.println(a.compareTo(10));

Output rules:

  • negative → if first is smaller
  • zero → if equal
  • positive → if first is greater

equals() Method

Compares wrapper object content logically.

java Integer a = 100; Integer b = 100; System.out.println(a.equals(b));

Primitive to String and String to Primitive

Wrapper classes are often used in data conversion.

Primitive to String

java int x = 50; String s = String.valueOf(x); System.out.println(s);

String to Primitive

java String s = "75"; int x = Integer.parseInt(s); System.out.println(x);

Wrapper Objects are Immutable

Like String, most wrapper objects are immutable. Once created, their internal value does not change.

Example:

java Integer a = 10; Integer b = a + 5; System.out.println(a); System.out.println(b);

Here, a remains unchanged. A new value is produced for b.

Wrapper Class Caching

Java caches certain wrapper objects, especially Integer values in the range -128 to 127.

Example

java Integer a = 100; Integer b = 100; System.out.println(a == b);

This may print:

text true

Because Java reuses cached objects.

But for Large Values

java Integer a = 1000; Integer b = 1000; System.out.println(a == b);

This may print:

text false

Therefore, wrapper objects should usually be compared with equals(), not ==.

Auto Boxing and Performance Consideration

Autoboxing and unboxing make code easier to read, but excessive use may create unnecessary objects and impact performance in high-frequency code.

Example:

java Integer sum = 0; for (int i = 0; i < 100000; i++) { sum += i; }

Here, repeated boxing and unboxing may happen internally.

In performance-critical code, primitives may be more efficient.

NullPointerException in Unboxing

One common danger is unboxing a null wrapper object.

java Integer obj = null; int x = obj; // NullPointerException

Since Java tries to unbox null into a primitive, it throws an exception.

Important: Be careful when unboxing wrapper objects that may be null.

Real-World Example

Suppose we read marks from user input and store them in a list.

java import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList marks = new ArrayList(); marks.add(85); marks.add(90); marks.add(78); int total = 0; for (Integer mark : marks) { total += mark; // unboxing } System.out.println("Total = " + total); } }

Here:

  • primitive values are boxed while adding to list
  • wrapper values are unboxed while calculating total

Common Mistakes

  • using == instead of equals() for wrapper comparison
  • forgetting that collections require wrapper objects
  • confusing parseInt() with valueOf()
  • ignoring null values during unboxing
  • assuming wrapper objects behave exactly like primitives in all cases

Best Practices

  • use wrapper classes when working with collections and generics
  • use primitives for performance-critical numeric computations
  • use equals() for wrapper object comparison
  • be careful with null wrapper values
  • use utility methods like parseInt(), valueOf(), and toString() properly

Interview-Oriented Points

  • Wrapper classes represent primitive values as objects
  • Autoboxing converts primitive to wrapper automatically
  • Unboxing converts wrapper to primitive automatically
  • Collections require wrapper objects, not primitives
  • Integer.parseInt() returns primitive int
  • Integer.valueOf() returns Integer object
  • Wrapper classes are immutable
  • Unboxing null causes NullPointerException

Conclusion

Wrapper classes are an essential part of Java because they connect primitive data types with the object-oriented world of Java APIs, collections, and frameworks. Without them, many modern Java features would not work properly with primitive values.

Autoboxing and unboxing make this conversion smooth and automatic, but developers should still understand what happens internally. A strong understanding of wrapper classes helps write safer, cleaner, and more professional Java code.

Quick Summary: Wrapper classes convert primitive types into objects, autoboxing automatically wraps primitives into objects, and unboxing converts wrapper objects back into primitive values.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators