Skip to content

Commit 6aaf7bf

Browse files
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

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

Section20JAVA.lang.Package/src/WrapperClasses.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ public static void main(String[] args) {
33
int m1=15;
44

55
Integer m2=m1;
6-
76
Integer m3=15;
87

98
Integer m4= Integer.valueOf("123");
109
Integer m5= Integer.valueOf("11111", 2);
1110
Integer m6 =Integer.valueOf("A7", 15);
12-
1311
Integer m7=Integer.bitCount(9999);
12+
1413
System.out.println(m7);
1514

1615
String m8="1234";
@@ -31,11 +30,19 @@ public static void main(String[] args) {
3130
}
3231

3332
/*
34-
Note:
3533
Returns the number of one-bits in the two's complement binary representation of the specified int value.
3634
This function is sometimes referred to as the population count.
3735
38-
Params: i – the value whose bits are to be counted
39-
36+
Params: i – the value whose bits are to be counted.
4037
Returns: the number of one-bits in the two's complement binary representation of the specified int value.
41-
*/
38+
39+
1. Autoboxing → int m1 = 15; automatically Integer object ban jaata hai.
40+
2. Integer.valueOf(String, radix) → String ko specified base (radix) se Integer banata hai.
41+
- Example: "11111" base 2 → 31 decimal.
42+
- "A7" base 15 → decimal me convert hota hai.
43+
3. Integer.bitCount(n) → n ke binary representation me kitne 1-bits hai (population count).
44+
4. Integer.parseInt("1234") → String → int conversion.
45+
5. Integer.reverseBytes(x) → Byte order (endianness) reverse kar deta hai.
46+
6. Integer.toBinaryString(40) → decimal 40 ko binary string "101000" me convert karega.
47+
7. equals() vs == → equals() values compare karta hai, == reference check karta hai (wrapper classes me confuse ho sakta hai).
48+
*/

0 commit comments

Comments
 (0)