What is the main difference between StringBuffer
and StringBuilder
?
Is there any performance issues when deciding on any one of these?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Simply use
StringBuilder
unless you really are trying to share a buffer between threads.StringBuilder
is the unsynchronized (less overhead = more efficient) younger brother of the original synchronizedStringBuffer
class.StringBuffer
came first. Sun was concerned with correctness under all conditions, so they made it synchronized to make it thread-safe just in case.StringBuilder
came later. Most of the uses ofStringBuffer
were single-thread and unnecessarily paying the cost of the synchronization.Since
StringBuilder
is a drop-in replacement forStringBuffer
without the synchronization, there would not be differences between any examples.If you are trying to share between threads, you can use
StringBuffer
, but consider whether higher-level synchronization is necessary, e.g. perhaps instead of using StringBuffer, should you synchronize the methods that use the StringBuilder.StringBuilder
(introduced in Java 5) is identical toStringBuffer
, except its methods are not synchronized. This means it has better performance than the latter, but the drawback is that it is not thread-safe.Read tutorial for more details.
Basically,
StringBuffer
methods are synchronized whileStringBuilder
are not.The operations are "almost" the same, but using synchronized methods in a single thread is overkill.
That's pretty much about it.
Quote from StringBuilder API:
So it was made to substitute it.
The same happened with
Vector
andArrayList
.A simple program illustrating the difference between StringBuffer and StringBuilder:
There are no basic differences between StringBuilder and StringBuffer, only a few differences exist between them. In StringBuffer the methods are synchronized. This means that at a time only one thread can operate on them. If there are more than one thread then the second thread will have to wait for the first one to finish and the third one will have to wait for the first and second one to finish and so on. This makes the process very slow and hence the performance in case of StringBuffer is low.
On the other hand StringBuilder is non synchronized. This means that at a time multiple threads can operate on the same StrinBuilder object at the same time. This makes the process very fast and hence performance of StringBuilder is high.
StringBuffer is used to store character strings that will be changed (String objects cannot be changed). It automatically expands as needed. Related classes: String, CharSequence.
StringBuilder was added in Java 5. It is identical in all respects to StringBuffer except that it is not synchronized, which means that if multiple threads are accessing it at the same time, there could be trouble. For single-threaded programs, the most common case, avoiding the overhead of synchronization makes the StringBuilder very slightly faster.