? Java Wrapper Classes
? Quick Overview
Wrapper classes in Java convert primitive data types into objects. They allow primitives to be used in collections, APIs, and frameworks that require objects instead of raw values.
? Key Concepts
- Each primitive type has a corresponding wrapper class
- Wrapper classes belong to the
java.lang package
- They support methods and utilities unavailable to primitives
- Used heavily in Collections and Generics
? Syntax / Theory
- int → Integer
- double → Double
- char → Character
- boolean → Boolean
- Autoboxing converts primitive → object automatically
- Unboxing converts object → primitive automatically
? Code Example — Manual Boxing
? View Code Example
int a = 10;
Integer obj = Integer.valueOf(a);
System.out.println(obj);
? Code Example — Autoboxing & Unboxing
? View Code Example
Integer x = 20;
int y = x;
System.out.println(x + y);
? Live Output / Explanation
The wrapper object stores primitive values as objects. Java internally converts values during autoboxing and unboxing without explicit method calls.
✅ Tips & Best Practices
- Use wrapper classes when working with collections
- Avoid unnecessary boxing for performance-critical code
- Prefer
parseXxx() for string conversions
? Try It Yourself
- Create wrapper objects for all primitive types
- Compare
== vs equals() on Integer
- Convert String to Integer using wrapper methods