Difference Between StringBuffer and StringBuilder in Java

Strings are one of the most commonly used data types in Java, and Java provides multiple classes through which strings can be used. Two such classes are StringBuffer and StringBuilder, which provide an alternative to the immutable String class by creating mutable sequences of characters. In this article, we will discuss the differences between these two classes.

StringBuffer Class:

StringBuffer is a peer class of String that provides much of the functionality of strings. The string represents fixed-length, immutable character sequences while StringBuffer represents growable and writable character sequences. StringBuffer may have characters and substrings inserted in the middle or appended to the end. It will automatically grow to make room for such additions and often has more characters preallocated than are actually needed, to allow room for growth. In order to create a string buffer, an object needs to be created, for example:

java
StringBuffer str = new StringBuffer();

Here's an example that demonstrates how to use the StringBuffer class:

java
// Creating a new StringBuffer StringBuffer str = new StringBuffer("Hello"); // Appending to the StringBuffer str.append(" World!"); System.out.println(str);

Output:

Hello World!

StringBuilder Class:

Similar to StringBuffer, the StringBuilder in Java represents a mutable sequence of characters. The function of StringBuilder is very much similar to the StringBuffer class, as both of them provide an alternative to String Class by making a mutable sequence of characters. However, StringBuilder is not synchronized, which means that multiple threads can call the methods of StringBuilder simultaneously, making it faster than StringBuffer. In order to create a new string with the name str, we need to create an object of StringBuilder, for example:

java
StringBuilder str = new StringBuilder();

Here's an example that demonstrates how to use the StringBuilder class:

java
// Creating a new StringBuilder StringBuilder str = new StringBuilder("Hello"); // Appending to the StringBuilder str.append(" World!"); System.out.println(str);

Output:

Hello World!

Conversion from StringBuffer to StringBuilder:

The StringBuffer cannot be directly converted to StringBuilder. We first need to convert the StringBuffer to a String object by using the inbuilt method toString(). After converting it to a string object, we can simply create a StringBuilder using the constructor of the class. For example:

java
// Creating a new StringBuffer StringBuffer sbr = new StringBuffer("Geeks"); // Conversion from StringBuffer // object to the String object String str = sbr.toString(); // Creating a new StringBuilder // using the constructor StringBuilder sbl = new StringBuilder(str); System.out.println(sbl);

Output:

Geeks

Conversion from StringBuilder to StringBuffer:

Similar to the above conversion, the StringBuilder cannot be converted to the StringBuffer directly. We first need to convert the StringBuilder to the String object by using the inbuilt method toString(). Now, we can create a StringBuilder using the constructor. For example:

java
// Creating a new StringBuilder StringBuilder sbr = new StringBuilder("Geeks"); // Conversion from StringBuilder // object to the String object String str = sbr.toString(); // Creating a new StringBuffer // using the constructor StringBuffer sbl = new StringBuffer(str); System.out.println(sbl);

Output:

Geeks

StringBuilder vs StringBuffer in Java:

StringBuffer ClassStringBuilder Class
Present in JavaIntroduced in Java 5
Synchronized; multiple threads cannot call its methods simultaneouslyAsynchronized; multiple threads can call its methods simultaneously
Thread-safe classNot a thread-safe class
Slower than StringBuilder due to synchronizationFaster than StringBuffer due to no preliminary check for multiple threads

In conclusion, both StringBuffer and StringBuilder classes offer similar functionalities and provide mutable character sequences. However, they differ in their synchronization properties, with StringBuffer being synchronized and thus thread-safe, while StringBuilder is not synchronized and thus not thread-safe.

When choosing between these classes, it is important to consider the use case and the requirements of the application. If the application involves multiple threads accessing and modifying the same string concurrently, then StringBuffer should be used to ensure thread safety. On the other hand, if thread safety is not a concern and the application requires high performance, then StringBuilder should be used due to its faster execution speed.

Additionally, it is important to note that both classes have their own set of methods for manipulating strings, such as append(), insert(), delete(), replace(), etc. These methods can be used to modify the content of the string in place, without creating a new object every time, resulting in improved performance.

In conclusion, the choice between StringBuffer and StringBuilder in Java depends on the specific needs of the application. While StringBuffer provides thread safety, it comes at the cost of slower execution, whereas StringBuilder offers faster execution but does not provide thread safety. Understanding the differences between these classes can help developers make informed decisions when working with strings in Java.

Previous Post Next Post