Skip to content

Commit 426bfb3

Browse files
committed
feat: add WrapperDemo3 to demonstrate Character and Boolean wrapper class behavior
WHAT was added: - Example with `Character` wrapper: - Conversion from lowercase to uppercase using `Character.toUpperCase()`. - Conversion of `Character` to `String` using `Character.toString()`. - Notes on immutability of wrapper classes (Character, Boolean, etc.). KEY LEARNINGS: 1. Immutability of Wrappers: - Wrapper classes in Java (`Character`, `Boolean`, `Integer`, etc.) are immutable. - Any transformation method (e.g., `toUpperCase`) does NOT modify the existing object; instead, it returns a new value. - Example: ```java Character a1 = 'a'; Character.toUpperCase(a1); // returns 'A' but doesn't change a1 a1 = Character.toUpperCase(a1); // must assign back ``` 2. Character Class Usage: - `Character.toUpperCase('a') → 'A'` - `Character.toString('A') → "A"` (String object) - Shows how wrapper methods provide utility functions for conversions. 3. Boolean Wrapper Insights: - Boolean is also immutable. - Examples (not shown in code but relevant): - `Boolean.valueOf("true")` → `true` - `Boolean.parseBoolean("false")` → `false` - Useful for parsing string inputs into boolean values. REAL-WORLD APPLICATIONS: - ✅ Text processing: Converting user input to uppercase/lowercase before validation. - ✅ Data validation: Converting character/boolean values safely in web forms or config files. - ✅ String formatting: Wrapper methods help ensure proper case transformations in reporting tools. - ✅ Immutable behavior: Prevents accidental side effects in multi-threaded systems. RULE OF THUMB: - Always capture the returned value from wrapper utility methods. - Wrapper classes = immutable; they behave like `String` in Java. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent e78d905 commit 426bfb3

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

Section20JAVA.lang.Package/src/WrapperDemo3.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
public class WrapperDemo3 {
22
public static void main(String[] args) {
3-
//Character Class
3+
//Character Class.
44
Character a1='a';
55
a1 = Character.toUpperCase(a1);
6+
67
System.out.println(a1);
78

89
String str = Character.toString(a1);
@@ -14,9 +15,7 @@ public static void main(String[] args) {
1415
System.out.println(a1);
1516
1617
Why doesn't a1 change?
17-
1818
Immutability: The Character class is immutable, meaning its value cannot be changed once assigned.
19-
2019
Return Value Not Used: Character.toUpperCase(a1) returns a new character ('A') but does not modify a1 in place.
2120
2221
How to Fix It?
@@ -29,10 +28,29 @@ You need to store the returned value from toUpperCase(a1),like this:
2928
Similarly, Character.toString(a1) returns a String representation of a1, but does not modify a1.
3029
*/
3130

32-
//Boolean Class
31+
//Boolean Class.
3332
Boolean b;
34-
35-
//Boolean.
36-
3733
}
3834
}
35+
36+
/*
37+
1. Character Wrapper Class immutable hai → ek baar object ban gaya to uski value
38+
change nahi hoti. Har method naya value return karega.
39+
40+
2. Example: Character.toUpperCase('a') → 'A' return karega,
41+
lekin original variable tabhi update hoga jab tum explicitly
42+
usse assign karoge:
43+
a1 = Character.toUpperCase(a1);
44+
45+
3. Character.toString(a1) → ek String banata hai (original Character ko
46+
modify nahi karta).
47+
48+
4. Boolean Wrapper Class bhi immutable hai.
49+
- Boolean.valueOf("true") → String ko Boolean object me convert karega.
50+
- print karne pe directly "true" ya "false" dikhata hai.
51+
52+
5. Rule of Thumb:
53+
- Wrapper classes (Integer, Character, Boolean, etc.) sab immutable hote hain.
54+
- Har operation me naya object return hota hai,
55+
original variable change nahi hota jab tak tum explicitly assign na karo.
56+
*/

0 commit comments

Comments
 (0)