Commit c5ad32f
committed
feat: demonstrate conversion of String to StringBuffer and StringBuilder with operations
WHAT was added:
- Implemented `StringConversion` class to show interoperability between `String`, `StringBuffer`, and `StringBuilder`.
- Converted a `String` ("Geeks") into:
- `StringBuffer` → used `reverse()` to showcase mutability and modification in place.
- `StringBuilder` → used `append("forGeeks")` to demonstrate efficient concatenation.
WHY this matters:
- Emphasizes that while `String` is immutable, both `StringBuffer` and `StringBuilder` are mutable.
- Demonstrates how existing `String` data can be wrapped in mutable alternatives for efficient modifications.
PROGRAM OUTPUT:
- `skeeG` (reversed using `StringBuffer`)
- `GeeksforGeeks` (appended using `StringBuilder`)
REAL-WORLD APPLICATIONS:
- **StringBuffer reverse()** → used in algorithms like palindrome checkers, cryptography, or text editors where reversing is needed.
- **StringBuilder append()** → ideal for building large dynamic strings (e.g., SQL queries, JSON/XML builders, log messages).
- **Conversion** → allows starting with immutable `String` (safe input) and switching to mutable types for processing.
KEY TAKEAWAYS:
1. `StringBuffer` and `StringBuilder` constructors accept a `String` for easy conversion.
2. `StringBuffer` is synchronized → thread-safe but slower.
3. `StringBuilder` is non-synchronized → faster in single-threaded contexts.
4. Choose based on performance vs. concurrency needs.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent b08c255 commit c5ad32f
1 file changed
+25
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | 1 | | |
4 | | - | |
5 | 2 | | |
6 | | - | |
7 | | - | |
8 | 3 | | |
9 | 4 | | |
10 | | - | |
11 | | - | |
| 5 | + | |
12 | 6 | | |
13 | 7 | | |
14 | | - | |
| 8 | + | |
15 | 9 | | |
16 | | - | |
17 | | - | |
18 | 10 | | |
19 | 11 | | |
20 | | - | |
| 12 | + | |
21 | 13 | | |
22 | 14 | | |
23 | | - | |
| 15 | + | |
24 | 16 | | |
25 | 17 | | |
26 | | - | |
| 18 | + | |
27 | 19 | | |
28 | 20 | | |
29 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
0 commit comments