Skip to content

Commit 05afff4

Browse files
committed
Section 23 Java IO Streams
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 631d366 commit 05afff4

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

Section23JavaIOStreams/String Builder Vs String Buffer.txt renamed to Section20JAVA.lang.Package/src/String Builder Vs String Buffer.txt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
The key differences between StringBuffer and StringBuilder in Java are:
22

3-
43
| Feature | StringBuffer | StringBuilder |
54
|---------------|--------------------------------------------------------------------|----------------------------------------------------------------------|
65
| Thread Safety | Yes, it is synchronized and thread-safe. | No, it is not synchronized and not thread-safe. |
@@ -10,7 +9,7 @@ The key differences between StringBuffer and StringBuilder in Java are:
109

1110
Example:
1211

13-
Using StringBuffer (Thread-safe)
12+
Using StringBuffer (Thread-safe):
1413

1514
StringBuffer sb = new StringBuffer("Hello");
1615
sb.append(" World");
@@ -25,19 +24,18 @@ sb.append(" World");
2524
System.out.println(sb); // Output: Hello World
2625

2726
Which one to use?
27+
2828
- If thread safety is required → Use StringBuffer.
2929
- If thread safety is not a concern and better performance is needed → Use StringBuilder.
3030

3131

3232
When to Use StringBuffer and StringBuilder
3333

3434
✅ Use StringBuffer when:
35-
3635
1. You are working in a multi-threaded environment where multiple threads might access and modify the same string.
3736
2. You need synchronization to avoid concurrency issues.
3837
3. You are handling shared mutable strings and want to ensure thread safety.
3938

40-
4139
Example: Multi-threaded scenario with StringBuffer
4240

4341
class StringBufferExample {
@@ -52,9 +50,7 @@ class StringBufferExample {
5250
}
5351
}
5452

55-
56-
✅ Use StringBuilder when:
57-
53+
Use StringBuilder when:
5854
1. You are working in a single-threaded environment.
5955
2. You need better performance since StringBuilder is faster than StringBuffer.
6056
3. You don't need synchronization or thread safety.
@@ -71,13 +67,14 @@ class StringBuilderExample {
7167

7268
Performance Comparison
7369

74-
If thread safety is not required, always prefer StringBuilder over StringBuffer because it avoids unnecessary synchronization and runs faster.
70+
If thread safety is not required, always prefer StringBuilder over StringBuffer because it avoids unnecessary
71+
synchronization and runs faster.
7572

76-
Choosing the Right One:
73+
Choosing the Right One:
7774

7875
| Scenario | Recommendation |
7976
|-----------------------------------------------------------|----------------|
8077
| Single-threaded application | StringBuilder |
8178
| Multi-threaded application | StringBuffer |
8279
| Performance is a priority & thread safety is not required | StringBuilder |
83-
| Working with multiple threads on the same string object | StringBuffer |
80+
| Working with multiple threads on the same string object | StringBuffer |

0 commit comments

Comments
 (0)