Skip to content

Commit fe415c0

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

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed
Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
1-
/*
2-
This conversion can be performed using toString() method which is overridden in both StringBuffer and StringBuilder classes. Below is the Java program to demonstrate the same.
3-
4-
Note: While we use toString() method, a new String object(in Heap area) is allocated and initialized to the character sequence currently represented by the StringBuffer object, which means the subsequent changes to the StringBuffer object do not affect the contents of the String object.
5-
6-
Example:
7-
Java Program to Demonstrate Conversion from
8-
String to StringBuffer and StringBuilder
9-
*/
101
public class SBvsSB {
112

123
public static void main(String[] args) {
13-
14-
// Creating objects of
15-
// StringBuffer class
4+
// Creating objects of StringBuffer class.
165
StringBuffer sbr = new StringBuffer("Geeks");
176
StringBuilder sbl = new StringBuilder("Hello");
187

19-
// Converting StringBuffer object to String
20-
// using toString() method
8+
// Converting StringBuffer object to String, using toString() method.
219
String s1 = sbr.toString();
2210

23-
// Printing the above string
24-
System.out.println(
25-
"StringBuffer object to String: ");
11+
System.out.println("StringBuffer object to String: ");
12+
2613
System.out.println(s1);
2714

28-
// Converting StringBuilder object to String
15+
// Converting StringBuilder object to String.
2916
String s2 = sbl.toString();
3017

31-
// Printing the above string
32-
System.out.println(
33-
"StringBuilder object to String: ");
18+
System.out.println("StringBuilder object to String: ");
19+
3420
System.out.println(s2);
3521

36-
// Changing StringBuffer object sbr
37-
// but String object(s1) doesn't change
22+
// Changing StringBuffer object sbr, but String object(s1) doesn't change
3823
sbr.append("ForGeeks");
3924

40-
// Printing the above two strings on console
4125
System.out.println(sbr);
4226
System.out.println(s1);
4327
}
44-
}
28+
}
29+
30+
/*
31+
This conversion can be performed using toString() method which is overridden in both
32+
StringBuffer and StringBuilder classes.
33+
34+
Above, Java program to demonstrate the same.
35+
36+
Note: While we use toString() method, a new String object(in Heap area) is allocated and
37+
initialized to the character sequence currently represented by the StringBuffer object,
38+
which means the subsequent changes to the StringBuffer object do not affect the contents of the String object.
39+
40+
Example:
41+
Java Program to Demonstrate Conversion from String to StringBuffer and StringBuilder
42+
*/

0 commit comments

Comments
 (0)