Skip to content

Commit b08c255

Browse files
committed
feat: demonstrate immutability of String vs mutability of StringBuffer and StringBuilder
WHAT was added: - Implemented `StringBufferBuilder` to compare `String`, `StringBuffer`, and `StringBuilder`. - Showed how `concat()` on `String` creates a new object (ignored in this case). - Demonstrated `append()` on `StringBuffer` and `StringBuilder`, which directly modify the same object. WHY this matters: - Highlights the **core difference**: - `String` → immutable (operations create new objects). - `StringBuffer` → mutable + thread-safe (synchronized). - `StringBuilder` → mutable + faster (non-synchronized). - Helps choose the right class depending on **immutability, performance, and concurrency needs**. PROGRAM OUTPUT: - String → `Hello` (unchanged, since concat() result was not stored). - StringBuffer → `Hello World` (append modifies original). - StringBuilder → `Hello World` (append modifies original). REAL-WORLD APPLICATIONS: - **String** → Constants, configuration values, keys where immutability ensures safety. - **StringBuilder** → Efficient text manipulation in single-threaded apps (e.g., report generation, JSON building). - **StringBuffer** → Safe for concurrent modifications in multi-threaded contexts (e.g., shared logging systems). KEY TAKEAWAYS: 1. `String` is immutable → safe but slower for repeated modifications. 2. `StringBuffer` is mutable & synchronized → safer in multi-threaded use. 3. `StringBuilder` is mutable & non-synchronized → fastest in single-threaded use. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent fe415c0 commit b08c255

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed
Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
11
public class StringBufferBuilder {
22
public static void main(String[] args) {
3-
String s1 =new String("Hello");
43

4+
String s1 =new String("Hello");
55
StringBuffer s2 = new StringBuffer("Hello");
6-
76
StringBuilder s3 = new StringBuilder("Hello");
87

98
s1.concat(" World");
10-
119
s2.append(" World");
12-
1310
s3.append(" World");
1411

1512
System.out.println(s1);
16-
1713
System.out.println(s2);
18-
1914
System.out.println(s3);
2015
}
21-
}
16+
}
17+
18+
/*
19+
1. String (Immutable):
20+
- Jab bhi hum concat(), replace(), etc. use karte hain,
21+
ek naya String object ban jata hai.
22+
- Purana string object modify nahi hota.
23+
- Example: "Hello" + " World" → naya object create hoga.
24+
25+
2. StringBuffer (Mutable + Thread-safe):
26+
- append(), insert(), delete() → directly existing object me changes karta hai.
27+
- Synchronization ki wajah se multi-threaded environment me safe hai,
28+
but performance thoda slow hota hai.
29+
30+
3. StringBuilder (Mutable + Faster):
31+
- Almost same as StringBuffer but NOT synchronized.
32+
- Isliye ye single-threaded use case me fastest option hai.
33+
34+
4. Output of this program:
35+
- s1 → "Hello" (kyunki String immutable hai aur concat() ka result ignore kiya gaya)
36+
- s2 → "Hello World" (append ne existing object update kar diya)
37+
- s3 → "Hello World" (append ne existing object update kar diya)
38+
39+
- Use String → jab value kabhi change nahi hogi.
40+
- Use StringBuilder → jab frequent modifications single-threaded environment me karne hain.
41+
- Use StringBuffer → jab multi-threaded environment me modifications karne hain.
42+
*/

0 commit comments

Comments
 (0)