Commit 6aaf7bf
committed
feat: demonstrate usage of Java Wrapper Classes (Integer)
WHAT was added:
- Implemented `WrapperClasses` example showcasing:
- Autoboxing: primitive `int` to `Integer` (`m1 → m2`).
- `Integer.valueOf()` with different overloads:
- From String ("123") → decimal value.
- From String with radix ("11111", 2) → binary → decimal.
- From String with radix ("A7", 15) → base-15 conversion.
- `Integer.bitCount(int)` → counts number of 1-bits in binary representation.
- `Integer.parseInt(String)` → converts numeric string to primitive int.
- `Integer.reverseBytes(int)` → reverses the order of bytes.
- `Integer.toBinaryString(int)` → returns binary string representation.
- `equals()` check to compare `Integer` objects.
WHY this matters:
- Demonstrates how wrapper classes bridge primitives and objects, allowing usage in collections, generics, and utility APIs.
- Shows how `Integer` provides powerful static helper methods for binary operations, parsing, and conversions.
KEY TAKEAWAYS:
- Wrapper classes support **autoboxing/unboxing**: `int ↔ Integer`.
- `valueOf()` supports **custom base (radix)** conversions.
- `bitCount()` → population count, useful in low-level bit manipulation and optimization algorithms.
- `reverseBytes()` → critical in **endian conversions** when dealing with networking or file I/O.
- Wrapper methods like `toBinaryString()`, `parseInt()` simplify working with number formats.
REAL-WORLD APPLICATIONS:
- ✅ Parsing user input from text into numbers (`parseInt`).
- ✅ Working with binary/hex formats in **encryption, compression, or bitmasking** (`bitCount`, `toBinaryString`).
- ✅ Handling **network byte order (endianness)** with `reverseBytes`.
- ✅ Using wrappers in **collections (e.g., List<Integer>)** where primitives are not allowed.
- ✅ Converting between number bases (binary, octal, hex) for **compiler design or protocol parsing**.
PERFORMANCE NOTE:
- Autoboxing/unboxing introduces overhead; prefer primitives in performance-critical code.
- Use wrapper classes when objects are required (e.g., generics, nullability, utilities).
RULE OF THUMB:
- Use **primitives** for raw performance.
- Use **wrapper classes** for object-oriented features, conversions, and APIs requiring objects.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 73503b8 commit 6aaf7bf
1 file changed
+13
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
10 | 9 | | |
11 | 10 | | |
12 | | - | |
13 | 11 | | |
| 12 | + | |
14 | 13 | | |
15 | 14 | | |
16 | 15 | | |
| |||
31 | 30 | | |
32 | 31 | | |
33 | 32 | | |
34 | | - | |
35 | 33 | | |
36 | 34 | | |
37 | 35 | | |
38 | | - | |
39 | | - | |
| 36 | + | |
40 | 37 | | |
41 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
0 commit comments