Commit d7fb03c
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
1 file changed
+44
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
1 | 10 | | |
2 | | - | |
3 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
4 | 17 | | |
5 | | - | |
6 | 18 | | |
| 19 | + | |
7 | 20 | | |
8 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
9 | 24 | | |
10 | | - | |
11 | | - | |
12 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
13 | 30 | | |
14 | | - | |
15 | | - | |
16 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
17 | 34 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
0 commit comments