Introduction
In Java, everything is object-oriented β but primitive data types like int, double, char, and boolean are not objects β
This creates a problem.
What if we want to:
- Store numbers inside a collection like
ArrayList? π¦ - Convert numbers into strings? π
- Use utility methods like parsing and comparison?
- Treat primitive values as objects?
To solve this, Java provides Wrapper Classes π―
Wrapper classes allow primitive data types to be used as objects.
Letβs understand this concept in detail.
π§ What Are Wrapper Classes?
A Wrapper Class is a class that wraps (encloses) a primitive data type inside an object.
Each primitive type has a corresponding wrapper class.
π Primitive Types and Their Wrapper Classes
| Primitive | Wrapper Class |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char | Character |
| boolean | Boolean |
π― Why Do We Need Wrapper Classes?
Primitive types:
- β Are not objects
- β Cannot be stored in collections directly
- β Do not have methods
Wrapper classes:
- β Are objects
- β Can be stored in collections
- β Provide useful methods
π’ Example 1: Using Integer Wrapper
public class WrapperDemo1 {
public static void main(String[] args) {
int num = 10;
Integer obj = Integer.valueOf(num);
System.out.println("Primitive value: " + num);
System.out.println("Wrapper object: " + obj);
}
}
π§ Step-by-Step Explanation
numis primitiveInteger.valueOf(num)converts primitive into objectobjis an Integer object
This process is called Boxing π¦
π Boxing and Unboxing
π¦ Boxing
Converting primitive β object
int a = 20;
Integer obj = Integer.valueOf(a);
π€ Unboxing
Converting object β primitive
Integer obj = 30;
int a = obj.intValue();
β¨ Autoboxing and Auto-Unboxing
Since Java 5, conversion happens automatically.
π’ Example 2: Autoboxing
public class AutoBoxingDemo {
public static void main(String[] args) {
int num = 50;
Integer obj = num; // Autoboxing
System.out.println(obj);
}
}
π’ Example 3: Auto-Unboxing
public class AutoUnboxingDemo {
public static void main(String[] args) {
Integer obj = 100;
int num = obj; // Auto-unboxing
System.out.println(num);
}
}
π¦ Wrapper Classes in Collections
Collections like ArrayList store objects only.
π’ Example 4: Using ArrayList
import java.util.ArrayList;
public class CollectionDemo {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
System.out.println(list);
}
}
π§ Explanation
ArrayList<Integer>stores Integer objects10is autoboxed automatically
Primitive int cannot be stored directly β
π’ Important Wrapper Class Methods
π Integer Methods
Integer.parseInt("123"); // String β int
Integer.toString(100); // int β String
Integer.max(10, 20); // Maximum value
π’ Example 5: Parsing String
public class ParseDemo {
public static void main(String[] args) {
String str = "200";
int num = Integer.parseInt(str);
System.out.println(num + 50);
}
}
π§ Explanation
- Converts string to integer
- Useful for user input handling
π Comparing Wrapper Objects
β Important Concept
Integer a = 100;
Integer b = 100;
System.out.println(a == b);
This may return true because of Integer caching.
But:
Integer a = 200;
Integer b = 200;
System.out.println(a == b);
This may return false.
π Always use:
a.equals(b);
β Common Mistakes
β Using == for comparison
if(a == b) // Wrong
Use:
if(a.equals(b))
β NullPointerException during unboxing
Integer obj = null;
int num = obj; // β Runtime error
β Forgetting parsing exception
Integer.parseInt("abc"); // NumberFormatException
π§ Memory Concept
- Primitive stored in stack
- Wrapper objects stored in heap
Autoboxing creates object in heap memory.
π― When Should You Use Wrapper Classes?
β When working with collections
β When converting String to numbers
β When using utility methods
β When null values are required
β When working with generics
π Wrapper vs Primitive
| Primitive | Wrapper |
|---|---|
| Faster | Slightly slower |
| No methods | Has methods |
| Stored in stack | Stored in heap |
| Cannot store null | Can store null |
π₯ Interview/Viva Important Points
- Wrapper classes are immutable
- Integer caching occurs between -128 to 127
- Autoboxing introduced in Java 5
- Collections work only with objects
π§ͺ Tricky Example
public class TrickyWrapper {
public static void main(String[] args) {
Integer a = 127;
Integer b = 127;
System.out.println(a == b); // true
Integer x = 128;
Integer y = 128;
System.out.println(x == y); // false
}
}
π§ Why?
Java caches Integer values between -128 and 127.
Beyond that range, new objects are created.
β Conclusion
Wrapper classes bridge the gap between primitive data types and objects in Java π
They allow:
- Object-based manipulation of primitive values
- Storage in collections
- Easy conversion between data types
- Access to useful utility methods
Understanding wrapper classes deeply improves:
- Collection usage
- Data handling
- Input processing
- Memory understanding
Mastering this topic strengthens your core Java foundation and prepares you for advanced concepts πͺ