Difference Between String Buffer and String Builder

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Video Tutorial

StringBuilder class

Overview

Strings are immutable in Java. This means we cannot manipulate the value of a String object. If we modify the value of a String object in Java, a new String object with the modified value will be created in the heap memory.

StringBuilder and StringBuffer classes are used to provide us with functionality of mutable Strings in Java. StringBuffer provides us with strings that are safe to use with multiple threads but this added functionality also makes it slower. StringBuilder lacks thread safety but because it does not have this added functionality, it has faster implementation.

Scope

This article aims to:

  • Explain StringBuilder and StringBuffer classes in Java.
  • Differentiate between StringBuffer and StringBuilder classes.
  • Illustrate the fact that StringBuffer is thread-safe and StringBuilder is thread-unsafe.

Introduction

The ability of an object that prevents any change to it once it is created is said to be immutability. This property might not excite anyone who knows programming. But the creator of Java, James Gosling said:

“I would use an immutable whenever I can.”

Strings in Java

Java program makes I/O operations only with strings. This makes strings an integral part of any java application. Whether it may be your name, roll no, address or educational background, everything is denoted using string. Even quantifiable entities like 12g, 50kg, 22oz and many more could all be denoted as strings.

String class is the most basic class provided to us by java. A string is a sequence of characters like “apple”, “pizza”, “1234” . String class enables us to declare and define a string object in our programs.

String objects are immutable in nature. Whenever a String object is manipulated it does not affect the original object rather it creates a new object and assigns the new manipulated value to it.

Below are the different ways to declare and define a String object in Java.

Syntax:

Example:

Input:

Output:

Java StringBuffer class

It provides us with a way to use mutable strings in Java. These strings are safe to be used by multiple threads simultaneously. In order to give this advantage to the StringBuffer, the implementation of this class becomes less time efficient.

Syntax:

Code:

Output:

Java StringBuilder class

StringBuilder class also provides us with mutable strings but here we lack thread safety. It cannot be used by multiple threads simultaneously. Since StringBuilder class is not applying this extra feature like StringBuffer, it is faster than StringBuffer class.

Syntax:

Code:

Output:

Difference Between StringBuffer and StringBuilder classes

StringBufferStringBuilder
Safe to be used by multiple threadsUnsafe to be used by multiple threads
Faster than String class due to mutability but slower than StringBuilder class as it allows simultaneous operations of multiple threads.Fastest among all as it allows mutability and does not allow multiple threads operating at the same time.
Introduced in JDK1.0Introduced in JDK1.5

StringBuffer and StringBuilder classes with multiple threads

In the code below:

  • We have created three threads each to manipulate objects of StringBuffer and StringBuilder classes.
  • For sbuilder (object of StringBuilder class), three threads are manipulating it simultaneously namely builderThread1, builderThread2, and builderThread3.
  • Similarly, for sbuffer (object of StringBuffer class), three threads are manipulating it simultaneously namely bufferThread1, bufferThread2, and bufferThread3.
  • Let us see the value of sbuilder and sbuffer objects when these threads are simultaneously operating on them.

Code:

Ouput on First Run:

Output on Second Run:

Output on Third Run:

Explanation:

  • The value of sbuilder is varying over different runs of the same program whereas sbuffer value remains the same.
  • This implies when bufferThread1 is operating on sbuffer, then bufferThread2 and bufferThread3 do not try to access or manipulate its value. This holds true in the case of bufferThread2 and bufferThread3 as well.
  • However, this is not the case with StringBuilder class. All the three threads simultaneously access try to access, manipulate and update the value of sbuilder object resulting in varying results.
  • This shows the fact that StringBuilder is thread unsafe whereas StringBuffer is thread-safe.

Performance Test of StringBuffer and StringBuilder in Java

The program below depicts how the StringBuilder Class is faster than the StringBuffer Class. We have created an object with String value “Scaler” using both these classes. Then we keep on adding a random string for multiple times and check the total time taken for the same task by both the strings.

The performance difference is mostly because StringBuffer methods are synchronized while StringBuilder are not. Added synchronization methods make StringBuffer safer to use than StringBuilder but it also makes it slower.

Code:

Output:

Conclusion

  • Strings are immutable in Java. Java provides StringBuffer and StringBuilder classes to create and operate on mutable Strings.
  • StringBuffer class is thread-safe. It implies multiple threads operating on shared data are synchronized with each other.
  • StringBuilder class is thread-unsafe. Multiple thread operations on shared data are not synchronized.
  • StringBuffer class operations is slower than StringBuilder class operations due to additional checks required for thread safety.