π 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
?
int
is a primitive data typeInteger
is a class that wraps anint
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 :