Skip to content

Commit 698a805

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

File tree

1 file changed

+50
-19
lines changed

1 file changed

+50
-19
lines changed
Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,61 @@
1-
public class WrapperClassesDemo
2-
{
3-
public static void main(String[] args)
4-
{
5-
//Some primitive types
1+
public class WrapperClassesDemo {
2+
public static void main(String[] args) {
3+
// Primitive data types
64
char letter = 'A';
75
boolean value = true;
86
int intNum = -269;
9-
double floatNum = Math.pow(2,2); // 2 to the 2 is 4
7+
double floatNum = Math.pow(2, 2); // 2^2 = 4.0
108

11-
//Converting them into objects via their wrapper classes
12-
Character Letter = (Character) letter;
13-
Boolean Value = (Boolean) value;
14-
Integer IntNum = (Integer) intNum;
15-
Double FloatNum = (Double) floatNum;
9+
// Autoboxing: Primitive → Wrapper Objects
10+
Character Letter = letter; // (Character.valueOf(letter))
11+
Boolean Value = value; // (Boolean.valueOf(value))
12+
Integer IntNum = intNum; // (Integer.valueOf(intNum))
13+
Double FloatNum = floatNum; // (Double.valueOf(floatNum))
1614

17-
//Wrapper objects print their value just fine
18-
System.out.println("" + Letter + " " + Value + " " + IntNum + " " + FloatNum);
15+
// Wrapper objects print their stored values
16+
System.out.println(Letter + " " + Value + " " + IntNum + " " + FloatNum);
1917

20-
//Some Numerical objects
18+
// More wrapper objects
2119
Long Num1 = 987L;
2220
Float Num2 = 34.56f;
2321

24-
//Turning them into primitive types
25-
long num1 = (long) Num1;
26-
float num2 = (float) Num2;
22+
// Unboxing: Wrapper → Primitive
23+
long num1 = Num1; // (Num1.longValue())
24+
float num2 = Num2; // (Num2.floatValue())
2725

28-
System.out.println("" + num1 + " " + num2);
26+
System.out.println(num1 + " " + num2);
2927
}
30-
}
28+
}
29+
30+
/*
31+
1. Autoboxing & Unboxing:
32+
- Primitive → Wrapper automatically ho jaata hai.
33+
Example: int x = 5; Integer y = x; // Autoboxing
34+
- Wrapper → Primitive bhi automatic hota hai.
35+
Example: Integer obj = 10; int z = obj; // Unboxing
36+
37+
2. Wrapper Classes ka kaam:
38+
- Primitive types ko Objects ki tarah treat karne ke liye use hote hain.
39+
- Useful in Collections (ArrayList<int> allow nahi hai, but ArrayList<Integer> chalega).
40+
41+
3. Common methods:
42+
- Integer.valueOf(String, radix) → String ko given base (radix) se integer me convert karta hai.
43+
Example:
44+
"11111" base 2 → 31 decimal
45+
"A7" base 15 → decimal 157
46+
- Integer.parseInt("1234") → String ko int me convert karta hai.
47+
48+
4. Special methods:
49+
- Integer.bitCount(n) → n ke binary representation me kitne 1-bits hai (population count).
50+
- Integer.reverseBytes(x) → Byte order reverse kar deta hai (endianness swap).
51+
- Integer.toBinaryString(40) → 40 ko binary string "101000" me convert karega.
52+
53+
5. equals() vs == :
54+
- equals() → object ke values compare karta hai.
55+
- == → reference (memory address) compare karta hai.
56+
- Wrapper classes me confusion ho sakta hai (Integer caching effect ke wajah se).
57+
58+
6. Performance Note:
59+
- Wrapper classes thread-safe nahi hote, sirf object wrappers hote hain.
60+
- Autoboxing zyada use karne se performance cost ho sakti hai (boxing/unboxing overhead).
61+
*/

0 commit comments

Comments
 (0)