πŸ“Š Java Data Structures
Estimated reading: 3 minutes 30 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Java Wrapper Classes

Or Copy Link

CONTENTS
Scroll to Top