
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:
Stringis immutable. AStringcan't be changed once it's made. If you change it, a newStringobject will be created. - Thread-Safe: Yes, being immutable,
Stringis inherently thread-safe. - Performance: As the
Stringis 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
StringBuilderdue 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:
StringBuilderis 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: