StringBuffer vs StringBuilder in Java

Last Updated : 28 May, 2026

StringBuffer and StringBuilder are classes in Java used to create and modify mutable strings. Unlike the String class, their content can be changed without creating a new object. They are mainly used when frequent string modifications such as append, insert, or delete operations are required.

  • StringBuffer is thread-safe and synchronized, whereas StringBuilder is not synchronized and not thread-safe.
  • StringBuffer is slower in performance, whereas StringBuilder is faster and more efficient for single-threaded applications.

StringBuffer Class

StringBuffer is a mutable sequence of characters that is thread-safe. It is designed for multi-threaded environments where multiple threads may modify the same string object.

  • StringBuffer is thread-safe because its methods are synchronized.
  • Used when multiple threads access the same object.
  • Safer for concurrent operations.

Syntax

StringBuffer sb = new StringBuffer("Hello");

Java
class Test {
    public static void main(String[] args) {

        StringBuffer sb1 = new StringBuffer("Hello");
        StringBuffer sb2 = sb1;

        sb1.append(" World");

        if (sb1 == sb2)
            System.out.println("Same");
        else
            System.out.println("Not Same");
    }
}

Output
Same

Explanation:

  • sb1 and sb2 refer to the same StringBuffer object
  • The append() method modifies the existing object
  • No new object is created during modification
  • Both references still point to the same memory location

StringBuilder

StringBuilder is a mutable sequence of characters similar to StringBuffer , but it is not thread-safe. It is optimized for single-threaded environments where performance is critical.

  • Suitable for single-threaded applications.
  • Does not use synchronized methods.
  • Faster execution and better performance.

Syntax

StringBuilder sb = new StringBuilder("Hello");

Java
class Test {
    public static void main(String[] args) {

        StringBuilder sb1 = new StringBuilder("Hello");
        StringBuilder sb2 = sb1;

        sb1.append(" World");

        if (sb1 == sb2)
            System.out.println("Same");
        else
            System.out.println("Not Same");
    }
}

Output
Same

Explanation:

  • sb1 and sb2 reference the same object
  • append() updates the existing StringBuilder instance
  • No new object is created after modification
  • Faster execution due to lack of synchronization

StringBuilder vs StringBuffer

Below is the key differences table of StringBuffer and StringBuilder.

FeatureStringBufferStringBuilder
Introduced InJava 1.0Java 1.5
Thread SafetyThread-safeNot thread-safe
PerformanceSlower due to synchronizationFaster because no synchronization
SynchronizationMethods are synchronizedMethods are not synchronized
Memory EfficiencySlightly less efficientMore memory efficient
Execution SpeedLower execution speedHigher execution speed
Suitable ForMulti-threaded environmentsSingle-threaded environments
Data SafetyProvides data consistency between threadsNo protection for concurrent access
UsageUsed when security and synchronization are requiredUsed when high performance is required
Preferred InBanking, server-side applicationsCompetitive coding, desktop applications
Method BehaviorSafe for shared objectsBest for local objects
Modification OperationsSupports mutable string operationsSupports mutable string operations
Object CreationChanges existing object without creating new oneChanges existing object without creating new one
Efficiency in LoopsLess efficient in loopsMore efficient in loops
Common Methodsappend(), insert(), delete(), reverse()append(), insert(), delete(), reverse()
Comment