Commit 698a805
committed
feat: add demo for Wrapper Classes and Autoboxing/Unboxing in Java
WHAT was added:
- Example `WrapperClassesDemo` showcasing:
- Conversion of primitive types (`char`, `boolean`, `int`, `double`) into wrapper objects (`Character`, `Boolean`, `Integer`, `Double`).
- Printing wrapper objects directly (they auto-display their contained values).
- Usage of `Long` and `Float` wrapper objects and converting them back into primitives (`long`, `float`).
WHY this matters:
- Demonstrates how wrapper classes act as object representations of primitive types.
- Shows both **autoboxing** (primitive → wrapper) and **unboxing** (wrapper → primitive).
- Highlights the flexibility of wrappers in allowing primitives to interact with Java Collections and APIs that require objects.
KEY TAKEAWAYS:
- Wrapper classes (Integer, Double, Boolean, Character, etc.) provide object versions of primitives.
- Autoboxing: Java automatically converts a primitive to its wrapper when needed.
Example: `int x = 5; Integer y = x;`
- Unboxing: Java automatically converts wrapper back to primitive.
Example: `Integer y = 10; int x = y;`
- Wrappers allow primitives to:
- Be stored in generic collections (`List<Integer>`).
- Use utility methods (`Integer.parseInt`, `Double.isNaN`, etc.).
- Handle `null` values (primitives cannot be `null`).
REAL-WORLD APPLICATIONS:
- ✅ Working with collections (`List<Integer>`, `Map<Character, Boolean>`).
- ✅ Parsing and converting numeric/string inputs (`Integer.parseInt("123")`).
- ✅ Supporting nullability in APIs (e.g., database frameworks may return `null` wrappers).
- ✅ Using built-in utilities for number conversion, formatting, or bit manipulation.
PERFORMANCE NOTE:
- Wrappers add slight overhead compared to primitives due to object allocation.
- Prefer primitives in performance-critical code; use wrappers when interacting with APIs, collections, or when null values are necessary.
RULE OF THUMB:
- Primitives → raw performance, no null.
- Wrappers → flexibility, nullability, and API/Collections compatibility.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 6aaf7bf commit 698a805
1 file changed
+50
-19
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
6 | 4 | | |
7 | 5 | | |
8 | 6 | | |
9 | | - | |
| 7 | + | |
10 | 8 | | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
16 | 14 | | |
17 | | - | |
18 | | - | |
| 15 | + | |
| 16 | + | |
19 | 17 | | |
20 | | - | |
| 18 | + | |
21 | 19 | | |
22 | 20 | | |
23 | 21 | | |
24 | | - | |
25 | | - | |
26 | | - | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
27 | 25 | | |
28 | | - | |
| 26 | + | |
29 | 27 | | |
30 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
0 commit comments