Difference between StringBuffer and StringBuilder

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

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 the 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.

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 the String value “Scaler” using both these classes. Then we keep on adding a random string 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 StringBuilderis 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 are slower than StringBuilder class operations due to additional checks required for thread safety.

See Also: