π 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 Type | Wrapper Class |
|---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
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
| Feature | Primitive | Wrapper Class |
|---|---|---|
| Stored in | Stack memory | Heap memory |
| Default values | Yes | null |
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?
intis a primitive data typeIntegeris a class that wraps anintvalue 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 :
