Commit fe415c0
committed
feat: add comparison between StringBuffer and StringBuilder with toString() conversion
WHAT was added:
- Implemented `SBvsSB` to demonstrate differences between `StringBuffer` and `StringBuilder`.
- Showed how to convert both to immutable `String` using `toString()`.
- Highlighted immutability of `String` vs mutability of buffer objects.
- Included comments explaining memory behavior and conversion process.
WHY this matters:
- Clarifies how mutable classes (`StringBuffer`, `StringBuilder`) interact with immutable `String`.
- Demonstrates that changes in buffer objects after conversion do not affect the `String` created.
- Reinforces understanding of when to use `StringBuffer` (thread-safe) vs `StringBuilder` (faster, single-threaded).
KEY LEARNINGS DEMONSTRATED:
1. **Conversion to String**
- Both `StringBuffer` and `StringBuilder` override `toString()` to create a new immutable `String`.
- Example:
```java
String s1 = sbr.toString(); // Converts buffer to String
```
2. **Immutability of String**
- Once converted, the new `String` object remains unchanged even if the buffer is modified later.
- Example:
```java
sbr.append("ForGeeks");
// sbr -> "GeeksForGeeks"
// s1 -> "Geeks" (unchanged)
```
3. **Thread Safety vs Performance**
- `StringBuffer`: Synchronized → safe in multithreading but slower.
- `StringBuilder`: Non-synchronized → faster but not thread-safe.
4. **Memory Behavior**
- `toString()` allocates a **new String in heap memory**.
- No shared references → further changes to buffer don’t affect the string.
REAL-WORLD APPLICATIONS:
- **StringBuffer**: Multithreaded loggers, concurrent text processing where synchronization is required.
- **StringBuilder**: Single-threaded string manipulations like XML/JSON building or string concatenation in compilers.
- **String Conversion**: When mutable text structures need to be passed as immutable data (e.g., API responses, DB queries).
LIMITATIONS:
- Frequent conversion between `String` and buffer objects may cause performance overhead due to new object creation.
- Use immutable `String` directly when no modifications are needed.
KEYWORDS:
StringBuffer vs StringBuilder, toString conversion, mutable vs immutable strings, thread safety, performance trade-offs.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent d7fb03c commit fe415c0
1 file changed
+23
-25
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | 1 | | |
11 | 2 | | |
12 | 3 | | |
13 | | - | |
14 | | - | |
15 | | - | |
| 4 | + | |
16 | 5 | | |
17 | 6 | | |
18 | 7 | | |
19 | | - | |
20 | | - | |
| 8 | + | |
21 | 9 | | |
22 | 10 | | |
23 | | - | |
24 | | - | |
25 | | - | |
| 11 | + | |
| 12 | + | |
26 | 13 | | |
27 | 14 | | |
28 | | - | |
| 15 | + | |
29 | 16 | | |
30 | 17 | | |
31 | | - | |
32 | | - | |
33 | | - | |
| 18 | + | |
| 19 | + | |
34 | 20 | | |
35 | 21 | | |
36 | | - | |
37 | | - | |
| 22 | + | |
38 | 23 | | |
39 | 24 | | |
40 | | - | |
41 | 25 | | |
42 | 26 | | |
43 | 27 | | |
44 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
0 commit comments