πŸ“Š Java Data Structures
Estimated reading: 3 minutes 427 views

🎁 Java Wrapper Classes Explained – Autoboxing, Examples & Use Cases


Introduction – Why Java Wrapper Classes Are Important

Java is a strictly object-oriented language, but its primitive data types like int, char, and double are not objects. This becomes a problem when working with collections (like ArrayList) or object-based APIs.

That’s where Wrapper Classes come in.

Java Wrapper Classes wrap primitive types into objects so they can be used in places where only objects are allowed.

By the end of this article, you’ll understand:

What wrapper classes are in Java
How autoboxing and unboxing work
Real-world use cases and differences with primitives
Best practices and performance considerations


What Are Wrapper Classes in Java?

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

Java provides 8 built-in wrapper classes in the java.lang package:

Primitive TypeWrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

These are immutable objects that provide utility methods and constants.


Basic Example – Primitive vs Wrapper

int num = 5; // primitive
Integer wrapped = Integer.valueOf(num); // wrapper object

System.out.println(wrapped); // Output: 5

Integer.valueOf() converts primitive int to Integer
wrapped is now an object of type Integer


Autoboxing & Unboxing in Java

Autoboxing

Automatic conversion of primitive β†’ wrapper:

Integer x = 100; // int to Integer automatically

Unboxing

Automatic conversion of wrapper β†’ primitive:

int y = x; // Integer to int automatically

Introduced in Java 5 to reduce boilerplate code


Real-World Example – ArrayList

You can’t use primitives in generic collections:

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

Use wrapper class:

ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10); // Autoboxed
int value = numbers.get(0); // Unboxed

Wrapper Class Utility Methods

Integer.parseInt() – Convert String to int

String s = "123";
int num = Integer.parseInt(s); // 123

Double.valueOf() – Convert String to Double

double d = Double.valueOf("45.6");

Boolean.parseBoolean()

boolean b = Boolean.parseBoolean("true");

Useful for reading user input, config files, and JSON/XML parsing


Comparison: Primitive vs Wrapper

FeaturePrimitiveWrapper Class
Stored inStack memoryHeap memory
Default valuesYesnull
Can be null No Yes
Used in collections No Yes
Methods available No Yes (compareTo(), etc.)
Performance Faster Slightly slower

Pitfalls to Avoid

NullPointerException

Integer a = null;
int b = a; //  NullPointerException during unboxing

== vs .equals()

Integer a = 128;
Integer b = 128;

System.out.println(a == b);       // false (different objects)
System.out.println(a.equals(b));  // true  (same value)

Use .equals() to compare values, not ==


Best Practices

Use primitive types when performance matters (e.g., loops, calculations)
Use wrapper classes for collections, nullability, and APIs
Avoid unnecessary boxing/unboxing in performance-sensitive areas
Prefer .equals() over == for comparing wrapper objects
Use valueOf() instead of new to leverage caching


Summary

  • Wrapper classes bridge the gap between primitives and objects
  • Java provides built-in wrappers for all primitive types
  • Autoboxing/unboxing makes conversion seamless
  • Wrappers are essential for working with collections, generics, and APIs
  • Use carefully in performance-critical code to avoid unnecessary overhead

FAQs – Java Wrapper Classes

Why do we need wrapper classes in Java?

To use primitive values where objects are required, like in collections, generics, and frameworks.

What is the difference between Integer and int?

  • int is a primitive data type
  • Integer is a class that wraps an int value as an object

Can I use wrapper classes in switch statements?

Yes, since Java 7, switch supports Byte, Short, Character, Integer, and String.

Are wrapper classes immutable?

Yes. All Java wrapper classes are final and immutable.

What is the default value of a wrapper object?

null. Unlike primitives, wrapper objects can be null.


Share Now :
Share

Java Wrapper Classes

Or Copy Link

CONTENTS
Scroll to Top