StringBuffer vs StringBuilder

When you are programming in the Java language, you will eventually come across StringBuffer and StringBuilder. Both StringBuffer and StringBuilder are classes in Java that are used to create a string object. At any point in time, a StringBuffer or StringBuilder object would contain a particular sequence of characters. Both StringBuffer and StringBuilder are superior to the regular String class because they are mutable objects. In other words, after you create an object using either StringBuffer or StringBuilder, you will still be able to change the length or content of the sequence by using certain method calls. A regular String object is immutable and cannot be changed after created.

So, what is the difference between StringBuffer and StringBuilder? When you are about to make a mutable object containing string, should you use StringBuffer or StringBuilder? The same question has been asked by many other developers before. Below, we will see the comparisons between StringBuffer and StringBuilder.

What is StringBuffer?

StringBuffer is a class used to create a thread-safe and mutable sequence of characters. StringBuffer is pretty much similar to String, except that it can be modified to change the length or content by using certain method calls. The special feature of StringBuffer is that it is synchronized and thus is thread-safe. StringBuffer objects are safe to be used by multiple threads because the methods are synchronized. The synchronization ensures that all operations on any instance behave as if they occur in a serial order that is consistent with the order of operations made by the other threads involved. In other words, two or more threads cannot call the methods of StringBuffer simultaneously – they must call the methods in turns, ensuring data integrity and coherence. StringBuffer was introduced for the first time in Java Development Kit 1.0.

What is StringBuilder?

StringBuilder is also a class used to create a mutable sequence of characters, which means that it can also be modified after it is created. However, it is not synchronized as thus is not thread-safe. This class provides an API that is compatible with StringBuffer, but with no guarantee of synchronization. Thus, it can be accessed by multiple threads simultaneously – compromising data integrity and coherence, most likely to result in a conflict. StringBuilder has been designed to be a drop-in replacement for StringBuffer in cases where the sequence of characters is only going to be used by a single thread.StringBuilder was introduced for the first time in Java Development Kit 1.5.

StringBuffer vs. StringBuilder: Thread Access

As mentioned above, the primary difference between StringBuffer and StringBuilder is that StringBuffer is synchronized and thread-safe, whereas StringBuilder is not synchronized and thus is not thread-safe. If you are creating something that is going to be accessed by just a single thread, without any possibility that it is going to be used by multiple threads simultaneously, you should use StringBuilder for the sake of performance. Of course, single thread access is much more common in today’s software development world. However, if you, for some reason, need to create something that is thread-safe to be accessed by multiple threads, you should use StringBuffer instead.

StringBuffer vs. StringBuilder: Storage Area

Unlike the regular String class, which stores the object in the Constant String Pool, both StringBuffer and StringBuilder store their objects in the heap, thus allowing them to be mutable. However, the overhead of StringBuffer is quite significantly larger than StringBuilder.

Of course, due to the slightly more complex nature, the overhead of StringBuffer is larger than that of StringBuilder. The overhead of StringBufferrequires the mechanism for performing synchronization. Such thing is not required in the overhead of StringBuilder. In the grand scheme of things, the overhead of StringBuffer is perhaps not a huge one. But, if compared to that of StringBuilder, the difference is quite significant.

StringBuffer vs. StringBuilder: Performance

In terms of performance, StringBuffer is considered less efficient than StringBuilder. Due to the synchronization, StringBuffer becomes much slower to perform. StringBuilder does not perform any synchronization and thus is much faster. That’s why StringBuilder is generally more recommended to use. StringBuffer should only be used when really necessary.

If you want to know further about the difference between StringBuffer and StringBuilder in terms of performance, you can try the following code in your system:

public class Test{
public static void main(String[] args){
longstart_time = System.currentTimeMillis();
StringBuffersb = new StringBuffer(“Java”);
for (int i=0; i<10000; i++){
sb.append(“Tpoint”);
}
System.out.println(“Time taken by the StringBuffer: ” + (System.currentTimeMillis() – start_time) + “ms”);

start_time = System.currentTimeMillis();
StringBuilder sb2 = new StringBuilder(“Java”);
for (int i=0; i<10000; i++){
sb2.append(“Tpoint”);
}
System.out.println(“Time taken by the StringBuilder: ” + (System.currentTimeMillis() –start_time) + “ms”);
}
}

The code above will produce the following output:
Time taken by the StringBuffer: 16ms
Time taken by the StringBuilder: 0ms

As you can see, the difference is quite significant. StringBuilder takes 0ms to perform the task, whereas StringBuffer takes as much as 16ms to perform the task. You can see that the task is a very simple one. Considering that the difference in performing a very simple task can be quite significant, we can assume that the gap can be even larger in performing a set of more complex commands.

Now, how about an existing application that does not need thread safety, but has been made using StringBuffer everywhere? Should you migrate all StringBuffer codes to StringBuilder? Well, if the application works properly and people are not complaining about performance, there is no need to migrate. You are not going to get so much from the extra time and effort required. But, if you are writing a new code or updating a code, you can convert them to StringBuilder at the same time.

StringBufferStringBuilder
- Synchronized and thread-safe, will prevent multiple threads accessing simultaneously- Not synchronized and not thread-safe, can be accessed by multiple threads simultaneously
- Much slower and less efficient- Significantly faster and more efficient
- Recommended only for cases requiring thread safety- Recommended when thread safety is not needed
- Less common to use- Much more common to use

Conclusion

Both StringBuffer and StringBuilder are mutable. However, StringBuffer is synchronized and is thread-safe, whereas StringBuilder is not synchronized and is not thread-safe. If only one thread is going to access the object at any point of time, you should use StringBuilder because it is much more efficient. However, if you are making something that can be accessed by multiple threads simultaneously, you should use StringBuffer.

Leave a Reply