Java Wrapper Classes-tutorial
In Java, primitive data types (like int, char, double) are fast and memory-efficient, but they are not objects. Sometimes, we need to treat primitive values like objects - for example, when working with collections such as ArrayList or HashMap that only store objects.
2. valueOf() – Convert Primitive or String to Wrapper Object
Example
3. datatypeValue() – Convert Wrapper Object to Primitive
Example
Output
4. toString() – Convert Primitive/Wrapper to String
To solve this, Java provides Wrapper Classes.
- A Wrapper Class wraps (converts) a primitive data type into an object.
- Example: int - Integer, double - Double, char - Character
This allows primitives to be used in object-based APIs.
What are Wrapper Classes?
Wrapper classes are classes that represent primitive data types as objects.
Each primitive type has a corresponding wrapper class in java.lang package.
Example
Output
Important Methods in Wrapper Classes
1. parsedatatype( ) – Convert String to Primitive
Example
Output
Primitive Type | Wrapper Class |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Example
int num = 10; // primitiveInteger obj = Integer.valueOf(num); // wrapper class objectSystem.out.println(obj);
Output
10
Why Do We Need Wrapper Classes?
- Collections Framework – Only objects can be stored in collections, not primitives.
- Utility Methods – Wrapper classes provide useful methods like parseInt(), isDigit(), etc.
- Object-Oriented Features – Wrappers can be used where objects are required.
- Type Conversions – Easy conversion between primitives, objects, and strings.
Important Methods in Wrapper Classes
1. parsedatatype( ) – Convert String to Primitive
Example
public class ParseExample { public static void main(String[ ] args) { String s = "100"; int num = Integer.parseInt(s); // parseInt() → int double d = Double.parseDouble("3.14"); // parseDouble() → double System.out.println("int: " + num); System.out.println("double: " + d); }}
Output
int: 100
double: 3.14
2. valueOf() – Convert Primitive or String to Wrapper Object
Example
Integer obj1 = Integer.valueOf(10); // from intInteger obj2 = Integer.valueOf("20"); // from StringDouble obj3 = Double.valueOf(3.14); // from double
3. datatypeValue() – Convert Wrapper Object to Primitive
Example
Integer obj = Integer.valueOf(50);int num = obj.intValue( ); // intValue() → intdouble d = Double.valueOf(3.14).doubleValue( ); // doubleValue() → doubleSystem.out.println(num);System.out.println(d);
Output
int: 100double: 3.14
4. toString() – Convert Primitive/Wrapper to String
Example
Output
Output
Output
int num = 200;String str = Integer.toString(num); // int → StringSystem.out.println("Converted String: " + str);
Output
Converted String: 200
Autoboxing in Java
Autoboxing is the process where the compiler automatically converts a primitive type into its corresponding wrapper class object. This feature was introduced in Java 5 to make coding easier and cleaner.
Example
Output
Example
Example
public class AutoBoxingDemo { public static void main(String[ ] args) { int num = 5; // primitive int Integer obj = num; // autoboxing (int → Integer) System.out.println("Primitive: " + num); System.out.println("Wrapper Object: " + obj); }}
Output
Primitive: 5Wrapper Object: 5
Unboxing in Java
Unboxing is the automatic conversion of a wrapper class object into its corresponding primitive type. It is the reverse of autoboxing.
Whenever you have a wrapper object, such as Integer, Double, or Boolean, and you need the primitive value, Java can automatically extract it for you without requiring any manual method call.
Example
public class UnBoxingDemo { public static void main(String[ ] args) { Integer obj = 20; // wrapper object int num = obj; // unboxing (Integer → int) System.out.println("Wrapper Object: " + obj); System.out.println("Primitive: " + num); }}
Output
Wrapper Object: 20Primitive: 20
Wrapper Classes in Collections (Real-Life Example)
Collections like ArrayList only work with objects, not primitives. Wrapper classes help here.
Example
Example
import java.util.*;
public class CollectionDemo { public static void main(String[ ] args) { ArrayList<Integer> list = new ArrayList<>( );
// Autoboxing happens automatically list.add(10); // int → Integer list.add(20); list.add(30);
// Unboxing while retrieving for (int n : list) { System.out.println(n); } }}
Output
102030
Key Point
- Wrapper classes convert primitives to objects and vice versa.
- Autoboxing = primitive --> wrapper (automatic).
- Unboxing = wrapper --> primitive (automatic).
- Collections and Generics need wrapper classes, not primitives.
- Wrapper classes provide useful methods (parseInt, toString, etc.).
Advantages
Works with Collections and Generics
Provides utility methods
Makes code more object-oriented
Limitations
Slight performance overhead (due to object creation)
Uses more memory compared to primitives
Two Minute Drill
- Java provides wrapper classes for every primitive type (int --> Integer, char --> Character, etc. )
- Autoboxing automatically converts primitive to wrapper.
- unboxing automatically converts wrapper to primitive.
- Needed for Collections (e.g., ArrayList<Integer>).
- Wrapper classes give extra features with methods.