Skip to content

Commit d7fb03c

Browse files
committed
feat: add example demonstrating StringBuffer usage and mutability
WHAT was added: - Implemented `SBClass` with a simple example of `StringBuffer`. - Created a `StringBuffer` initialized with `"Hello"`, appended `" World!"`, and printed the result. - Included explanatory comments on `StringBuffer` properties. WHY this matters: - Demonstrates the difference between immutable `String` and mutable `StringBuffer`. - Highlights thread-safety of `StringBuffer` compared to `StringBuilder`. - Shows practical usage of `append()` method for efficient string concatenation. KEY LEARNINGS DEMONSTRATED: 1. **Mutability of StringBuffer** - Unlike `String`, which is immutable, `StringBuffer` allows modification of the same object. - Example: ```java StringBuffer str = new StringBuffer("Hello"); str.append(" World!"); // str now contains "Hello World!" without creating a new object ``` 2. **Thread-safety** - All public methods in `StringBuffer` are synchronized. - Safe for use in multithreaded environments where multiple threads modify the same buffer. 3. **Performance trade-off** - Synchronization introduces overhead, making `StringBuffer` slower than `StringBuilder`. - For single-threaded use cases, `StringBuilder` is preferred. 4. **Important methods of StringBuffer** - `append(String s)` → concatenates string. - `insert(int offset, String s)` → inserts string at given position. - `delete(int start, int end)` → removes characters from buffer. - `reverse()` → reverses the sequence. - `capacity()` → returns current buffer capacity. REAL-WORLD APPLICATIONS: - **Multithreaded logging systems**: Safe string concatenation from multiple threads. - **Data processing pipelines**: Efficient manipulation of large text streams in concurrent environments. - **Legacy codebases**: Commonly found where thread-safe mutable strings were needed before `StringBuilder` became standard. LIMITATIONS: - Slower than `StringBuilder` in single-threaded scenarios due to synchronization overhead. - Overkill for simple immutable string operations. KEYWORDS: StringBuffer, mutable string, append, thread-safe string manipulation, Java concurrency. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 4d60bb9 commit d7fb03c

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed
Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,51 @@
1+
public class SBClass {
2+
public static void main(String args[]) {
3+
// Creating a new StringBuffer
4+
StringBuffer str = new StringBuffer("Hello");
5+
str.append(" World!");
6+
System.out.println(str);
7+
}
8+
}
9+
110
/*
2-
StringBuffer Class
3-
StringBuffer is a class that allows us to create and modify a flexible, expandable sequence of characters. It is thread-safe. Thismeans that it can be safely used when multiple threads are accessing or modifying the string concurrently. But due to synchronization, it is slower than StringBuilder.
11+
StringBuffer is a class that allows us to create and modify a flexible, expandable sequence of characters.
12+
13+
It is thread-safe.
14+
This means that it can be safely used when multiple threads are accessing or modifying the string concurrently.
15+
16+
But due to synchronization, it is slower than StringBuilder. StringBuffer str = new StringBuffer();
417
5-
StringBuffer str = new StringBuffer();
618
Example:
19+
Above is an example which implements the StringBuffer class. Java program to demonstrate the StringBuffer class
720
8-
Below is an example which implements the StringBuffer class.
21+
1. StringBuffer ek mutable sequence of characters hai.
22+
Matlab ek baar object create karne ke baad bhi uske andar ke content ko
23+
change (modify) kiya ja sakta hai bina naye object create kiye.
924
10-
Java program to demonstrate
11-
the StringBuffer class
12-
*/
25+
2. Thread-safety:
26+
- StringBuffer synchronized hoti hai.
27+
- Matlab multiple threads agar ek hi object ko access/modify karein
28+
to ye safe rehti hai.
29+
- Downside: synchronization ke wajah se ye thoda slow hoti hai.
1330
14-
public class SBClass {
15-
public static void main(String args[])
16-
{
31+
3. StringBuilder vs StringBuffer:
32+
- StringBuilder → NOT synchronized, faster, single-threaded use case ke liye best.
33+
- StringBuffer → Synchronized, thread-safe, multi-threaded use case ke liye best.
1734
18-
// Creating a new StringBuffer
19-
StringBuffer str = new StringBuffer("Hello");
20-
str.append(" World!");
21-
System.out.println(str);
22-
}
23-
}
35+
4. Commonly used methods:
36+
- append() → end me add karne ke liye
37+
- insert() → beech me insert karne ke liye
38+
- delete() → characters remove karne ke liye
39+
- reverse() → pura string ulta karne ke liye
40+
- capacity() → buffer capacity check karne ke liye
41+
- ensureCapacity() → minimum capacity set karne ke liye
42+
43+
5. Example flow:
44+
StringBuffer str = new StringBuffer("Hello");
45+
str.append(" World!");
46+
Output: Hello World!
47+
48+
Real-life use case:
49+
Jab tumhe string ke upar **bahut saare modifications** karne hain (like append, insert, delete in loops)
50+
aur multi-threaded environment hai, tab StringBuffer ek solid choice hai.
51+
*/

0 commit comments

Comments
 (0)