📘 Lesson · Lesson 34
Wrapper Classes & Autoboxing
Wrapper Classes & Autoboxing
What are Wrapper Classes?
Wrapper classes turn primitives into objects (int→Integer, double→Double). Needed for collections, which only store objects.
Autoboxing and Unboxing
Integer obj = 5; // autoboxing: int -> Integer
int x = obj; // unboxing: Integer -> int
ArrayList<Integer> list = new ArrayList<>();
list.add(10); // int auto-boxed into Integer
Primitive to Wrapper
| Primitive | Wrapper |
|---|---|
| int | Integer |
| double | Double |
| char | Character |
| boolean | Boolean |
Summary
- Wrapper classes make primitives into objects (int→Integer).
- Autoboxing converts automatically; needed for collections.
Wrapper Classes क्या हैं?
Wrapper classes primitives को objects बनाती हैं (int→Integer, double→Double)। Collections के लिए ज़रूरी, जो सिर्फ objects रखती हैं।
Autoboxing और Unboxing
Integer obj = 5; // autoboxing: int -> Integer
int x = obj; // unboxing: Integer -> int
ArrayList<Integer> list = new ArrayList<>();
list.add(10); // int auto-boxed into Integer
Primitive से Wrapper
| Primitive | Wrapper |
|---|---|
| int | Integer |
| double | Double |
| char | Character |
| boolean | Boolean |
सारांश
- Wrapper classes primitives को objects बनाती हैं (int→Integer)।
- Autoboxing अपने आप convert करता है; collections के लिए ज़रूरी।