Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between StringBuffer and StringBuilder class [duplicate]

What is the main difference between StringBuffer and StringBuilder? Is there any performance issues when deciding on any one of these?

like image 535
blacktiger Avatar asked Dec 10 '08 04:12

blacktiger


People also ask

What is the main difference between StringBuffer and StringBuilder?

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.

Are StringBuilder and String identical types?

StringBuilder and String are completely different objects.

How much faster is StringBuilder than StringBuffer?

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.

Why StringBuffer is slower than StringBuilder?

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.


4 Answers

StringBuffer is synchronized, StringBuilder is not.

like image 66
sblundy Avatar answered Sep 28 '22 12:09

sblundy


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.

like image 27
polygenelubricants Avatar answered Sep 28 '22 13:09

polygenelubricants


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.

like image 38
OscarRyz Avatar answered Sep 28 '22 13:09

OscarRyz


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.

like image 33
Bert F Avatar answered Sep 28 '22 12:09

Bert F