What is the main difference between StringBuffer
and StringBuilder
?
Is there any performance issues when deciding on any one of these?
String is immutable whereas StringBuffer and StringBuilder are mutable classes. StringBuffer is thread-safe and synchronized whereas StringBuilder is not. That's why StringBuilder is faster than StringBuffer.
StringBuilder and String are completely different objects.
So the figure displayed for StringBuilder is actually time it took to run stringbuffer AND stringbuilder test. Add this line and you'll see that StringBuilder IS faster about TWO TIMES.
In a synchronized environment, a single thread can perform a certain operation rather than disturbing any other thread that makes StringBuffer slower because it is synchronized. StringBuilder is not synchronized and that's why it is not thread-safe.
StringBuffer
is synchronized, StringBuilder
is not.
StringBuilder
is faster than StringBuffer
because it's not synchronized
.
Here's a simple benchmark test:
public class Main {
public static void main(String[] args) {
int N = 77777777;
long t;
{
StringBuffer sb = new StringBuffer();
t = System.currentTimeMillis();
for (int i = N; i --> 0 ;) {
sb.append("");
}
System.out.println(System.currentTimeMillis() - t);
}
{
StringBuilder sb = new StringBuilder();
t = System.currentTimeMillis();
for (int i = N; i > 0 ; i--) {
sb.append("");
}
System.out.println(System.currentTimeMillis() - t);
}
}
}
A test run gives the numbers of 2241 ms
for StringBuffer
vs 753 ms
for StringBuilder
.
Basically, StringBuffer
methods are synchronized while StringBuilder
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:
This class [StringBuilder] provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.
So it was made to substitute it.
The same happened with Vector
and ArrayList
.
But needed to get the clear difference with the help of an example?
StringBuffer or StringBuilder
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 synchronized StringBuffer
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 of StringBuffer
were single-thread and unnecessarily paying the cost of the synchronization.
Since StringBuilder
is a drop-in replacement for StringBuffer
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With