Skip to content

Commit c5ad32f

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

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed
Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
1-
// Java program to demonstrate conversion from
2-
// String to StringBuffer and StringBuilder
31
public class StringConversion {
4-
52
public static void main(String[] args) {
6-
7-
// Custom input string
83
String s = "Geeks";
94

10-
// Converting String object to StringBuffer object
11-
// by creating object of StringBuffer class
5+
// Converting String object to StringBuffer object by creating an object of StringBuffer class.
126
StringBuffer sbr = new StringBuffer(s);
137

14-
// Reversing the string
8+
// Reversing the string.
159
sbr.reverse();
16-
17-
// Printing the reversed string
1810
System.out.println(sbr);
1911

20-
// Converting String object to StringBuilder object
12+
// Converting String object to StringBuilder object.
2113
StringBuilder sbl = new StringBuilder(s);
2214

23-
// Adding it to string using append() method
15+
// Adding it to string using append() method.
2416
sbl.append("forGeeks");
2517

26-
// Print and display the above appended string
18+
// Print and display the above appended string.
2719
System.out.println(sbl);
2820
}
2921
}
22+
23+
/* Java program to demonstrate conversion from String to StringBuffer and StringBuilder.
24+
25+
1. String → Immutable (once created, modify nahi hota).
26+
Example: "Geeks" me kuch bhi add/modify karoge to new String object banega.
27+
28+
2. StringBuffer → Mutable + Thread-safe
29+
- Isliye reverse(), append(), insert(), delete() directly existing object pe kaam karte hain.
30+
31+
3. StringBuilder → Mutable + Faster (NOT synchronized)
32+
- Single-threaded applications me zyada fast hota hai.
33+
34+
4. Conversions:
35+
- String → StringBuffer: new StringBuffer(s)
36+
- String → StringBuilder: new StringBuilder(s)
37+
38+
5. Example Output:
39+
- Reversed StringBuffer: skeeG
40+
- Appended StringBuilder: GeeksforGeeks
41+
*/

0 commit comments

Comments
 (0)