Java Wrapper Classes
📦 Java Wrapper Classes
Wrapper Classes in Java are object representations of primitive data types.
-
They allow primitive types (
int,double, etc.) to be used as objects. -
Package:
java.lang -
Used in Collections, Generics, and utility methods that require objects.
✅ 1. Primitive vs Wrapper Classes
| Primitive Type | Wrapper Class |
|---|---|
byte |
Byte |
short |
Short |
int |
Integer |
long |
Long |
float |
Float |
double |
Double |
char |
Character |
boolean |
Boolean |
✅ 2. Why Use Wrapper Classes?
-
Collections: Collections like
ArrayListcan only store objects, not primitives. -
Utility Methods: Wrapper classes provide useful methods (e.g., parsing strings).
-
Autoboxing and Unboxing: Automatic conversion between primitives and objects.
🔹 3. Autoboxing and Unboxing
-
Autoboxing: Primitive → Wrapper Object
-
Unboxing: Wrapper Object → Primitive
🔹 4. Wrapper Class Methods
Parsing Strings to Primitives
Converting Wrapper to String
🔹 5. Some Useful Methods
| Wrapper Class | Useful Methods |
|---|---|
Integer |
parseInt(), valueOf(), toString(), MAX_VALUE, MIN_VALUE |
Double |
parseDouble(), valueOf(), toString(), MAX_VALUE, MIN_VALUE |
Boolean |
parseBoolean(), valueOf(), toString() |
Character |
isDigit(), isLetter(), toUpperCase(), toLowerCase() |
✅ 6. Example: Using Wrapper Classes in Collections
-
Collections cannot store primitives directly; Wrapper classes are required.
Summary
-
Wrapper Classes = Object form of primitives
-
Autoboxing & Unboxing make conversions seamless
-
Used in Collections, parsing, and utility methods
