StringBuffer vs StringBuilder

In this post, we will learn the difference between StringBuffer and StringBuilder in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.

What are StringBuffer and StringBuilder? 

Both StringBuffer and StringBuilder are classes in the Java standard library that are used to create and manipulate mutable sequences of characters. They're similar to String objects, but unlike String objects, they can be modified over time (i.e., they're mutable).

Difference between StringBuffer and StringBuilder in Java

StringBuffer StringBuilder
StringBuffer is synchronized, meaning it is thread-safe. Multiple threads can't access it simultaneously. StringBuilder is not synchronized, which means it is not thread-safe. Multiple threads can access it simultaneously.
Because of synchronization, StringBuffer is slower. StringBuilder is faster because it lacks synchronization.
StringBuffer is less efficient because it requires a lock before executing each operation. StringBuilder is more efficient because it does not require a lock before executing each operation.
It's better to use StringBuffer when multiple threads are working on the same String. StringBuilder is more suitable when one thread is working on the same String.
Introduced in JDK 1.0 Introduced in JDK 1.5
Methods in StringBuffer like append(), insert(), replace(), delete(), reverse() etc are synchronized. Methods in StringBuilder like append(), insert(), replace(), delete(), reverse() etc are not synchronized.

When to use StringBuffer vs StringBuilder? 

As a rule of thumb: 
  • Use StringBuffer when you need to create a mutable string in a multi-threaded environment. The synchronization offered by StringBuffer ensures that it remains safe when used across multiple threads. 
  • Use StringBuilder when you're working in a single-threaded environment, or thread safety isn't a concern. StringBuilder is less resource-intensive and offers better performance because it doesn't need to handle synchronization.

Examples

Let's create an example to illustrate the difference between StringBuffer and StringBuilder in Java. Both StringBuffer and StringBuilder are mutable classes that can be used for string manipulation. 

The key difference between them is that StringBuffer is thread-safe (synchronized), while StringBuilder is not. This means that StringBuilder is generally faster than StringBuffer, but StringBuffer is safer to use in multi-threaded environments. 

Example using StringBuffer:
public class StringBufferExample {
    public static void main(String[] args) {
        StringBuffer buffer = new StringBuffer("Hello");
        System.out.println("Original StringBuffer: " + buffer);

        // Appending a new value to the StringBuffer
        buffer.append(" World");
        System.out.println("Modified StringBuffer: " + buffer);

        // Performing another operation
        buffer.reverse();
        System.out.println("Modified StringBuffer (Reversed): " + buffer);
    }
}

Output:

Original StringBuffer: Hello
Modified StringBuffer: Hello World
Modified StringBuffer (Reversed): dlroW olleH
In the StringBuffer example, we create a StringBuffer with the value "Hello". We then use the append() method to add " World" to the buffer, and finally, we reverse the buffer using the reverse() method. All of these operations modify the buffer in place without creating new objects. The StringBuffer class is synchronized, so it ensures thread safety but can be slower compared to StringBuilder

Example using StringBuilder:
public class StringBuilderExample {
    public static void main(String[] args) {
        StringBuilder builder = new StringBuilder("Hello");
        System.out.println("Original StringBuilder: " + builder);

        // Appending a new value to the StringBuilder
        builder.append(" World");
        System.out.println("Modified StringBuilder: " + builder);

        // Performing another operation
        builder.reverse();
        System.out.println("Modified StringBuilder (Reversed): " + builder);
    }
}

Output:

Original StringBuilder: Hello
Modified StringBuilder: Hello World
Modified StringBuilder (Reversed): dlroW olleH
In the StringBuilder example, we perform the same operations as in the StringBuffer example. The StringBuilder class is not synchronized, which makes it faster than StringBuffer, but it is not thread-safe. Therefore, if you are working in a single-threaded environment or you manage synchronization explicitly, StringBuilder is generally preferred for better performance. 

Conclusion

In summary, both StringBuffer and StringBuilder are useful for string manipulation, but StringBuffer provides thread safety at the cost of potential performance overhead, while StringBuilder sacrifices thread safety for better performance. The appropriate choice depends on the requirements of your application and whether thread safety is a concern.

References


Comments