String vs StringBuffer vs StringBuilder
String vs StringBuffer vs StringBuilder 관련
Info
Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.
In this blog, we will learn about String
, StringBuffer
, and StringBuilder
.
As we know, we have String
, StringBuffer
, and StringBuilder
in both Java and Kotlin. So, whatever you are going to read now applies to both languages.
Let's begin.
String
, StringBuffer
, and StringBuilder
are completely different. We need to know the difference to avoid misusing them. They serve different purposes, especially when it comes to mutability, thread-safe, and performance.
We will start with the String.
String
- Mutability:
String
is immutable. AString
can't be changed once it's made. If you change it, a newString
object will be created. - Thread-Safe: Yes, being immutable,
String
is inherently thread-safe. - Performance: As the
String
is immutable, so it is slower for concatenation due to object creation. When we do frequent modifications, it can lead to performance issues due to the creation of many temporary objects. - Use case: Ideal for situations where strings don't require frequent modifications or when immutability is preferred.
Now that we have learned about the String
, it's time to learn about the StringBuffer
.
StringBuffer
- Mutability: StringBuffer is mutable. As it is mutable, it can be modified after it is created.
- Thread-Safe: Yes, it is thread-safe as its methods are synchronized. So, the advantage is that it can be used in multi-threaded environments.
- Performance: More efficient than String for frequent modifications, but slower than
StringBuilder
due to synchronization overhead. - Use case: Ideal for situations where strings require frequent modifications in multi-threaded environments.
Perfect, now let's jump into the StringBuilder
.
StringBuilder
- Mutability:
StringBuilder
is mutable. As it is mutable, it can be modified after it is created. - Thread-Safe: No, it is not thread-safe as its methods are not synchronized. So, the disadvantage is that it can't be used in multi-threaded environments.
- Performance: Most efficient for frequent string modifications in single-threaded environments.
- Use case: Ideal for situations where strings require frequent modifications in single-threaded environments.
We now understand the differences between String
, StringBuffer
, and StringBuilder
.
Let me tabulate the difference between String
, StringBuffer
, and StringBuilder
.
Feature | String | StringBuffer | StringBuilder |
---|---|---|---|
Mutability | Immutable. | Mutable. | Mutable. |
Thread-Safe | Yes. | Yes. | No. |
Performance | Slower for frequent modifications. | More efficient than String for frequent modifications, but slower than StringBuilder due to synchronization overhead. | Most efficient for frequent string modifications in single-threaded environments. |
Use case | Ideal for situations where strings don't require frequent modifications or when immutability is preferred. | Ideal for situations where strings require frequent modifications in multi-threaded environments. | Ideal for situations where strings require frequent modifications in single-threaded environments. |
This was all about String
, StringBuffer
, and StringBuilder
.
Prepare yourself for Android Interview: Android Interview Questions (amitshekhariitbhu/android-interview-questions
)
That's it for now.
Thanks
Amit Shekhar
You can connect with me on:
Follow Outcome School on: