GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — String vs StringBuilder vs StringBuffer in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

String vs StringBuilder vs StringBuffer in Java

Last Updated : 16 Oct, 2026

In Java, String, StringBuilder, and StringBuffer are used to store and manipulate sequences of characters. The main difference between them lies in mutability, thread safety, and performance. String is immutable, while StringBuilder and StringBuffer are mutable, making them suitable for different string manipulation requirements.

  • String: Immutable and suitable for text that does not require frequent modifications.
  • StringBuilder: Mutable and efficient for repeated string modifications without synchronization.
  • StringBuffer: Mutable and synchronized, making individual method calls thread-safe.

String

A String in Java is an immutable sequence of characters. Once a String object is created, its contents cannot be modified. Operations such as concat(), replace(), and substring() return a string result instead of changing the original object.

Java
public class StringExample{
    
    public static void main(String[] args){
        
        String str = "Hello";
        // creates a new object
        str.concat(" World");
        System.out.println(str); 
    }
}

Output
Hello

Explanation: Here, str.concat(" World") does not modify the original string. Instead, it creates a new String object, and the old one remains unchanged.

StringBuilder

StringBuilder is a mutable sequence of characters introduced in Java 5. Unlike String, it allows modifications to the existing object using methods such as append(), insert(), delete(), and reverse().

StringBuilder is not synchronized, making it generally suitable for repeated string modifications when external synchronization is not required.

Java
public class Geeks{
    
    public static void main(String[] args){
        
        StringBuilder sb = new StringBuilder("Hello");
        sb.append(" World");
        System.out.println(sb);
    }
}

Output
Hello World

Explanation: In the above example append() method modifies the existing object directly without creating a new one.

StringBuffer

StringBuffer is a mutable sequence of characters similar to StringBuilder. However, its main methods are synchronized, allowing individual operations to be performed safely when multiple threads access the same object. This synchronization introduces some overhead compared with StringBuilder.

Java
public class Geeks{
    
    public static void main(String[] args){
        
        StringBuffer sb = new StringBuffer("Hello");
        sb.append(" World");
        System.out.println(sb);
    }
}

Output
Hello World

Explanation: In the above example append() modifies the existing object safely in a multi-threaded environment.

Use Cases of String, StringBuilder, and StringBuffer

Geeks now you must be wondering when to use which one, do refer below as follows:

  • If a string is going to remain constant throughout the program, then use the String class object because a String object is immutable.
  • If a string can change (for example: lots of logic and operations in the construction of the string) and will only be accessed from a single thread, using a StringBuilder is good enough.
  • If a string can change and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous, so you have thread-safety.
  • If you don't want thread-safety than you can also go with StringBuilder class as it is not synchronized.

StringBuilder vs String vs StringBuffer

The table below demonstrates the difference between String, StringBuilder and StringBuffer:

Features

String

StringBuilder

StringBuffer

Mutability

Strings are immutable(creates new objects on modification)

StringBuilder are mutable(modifies in place)

StringBuffer are mutable (modifies in place)

Thread-Safe

It is thread-safe

It is not thread-safe

It is thread-safe

Performance

It is slow because it creates an object each time

It is faster (no object creation)

it is slower due to synchronization overhead

use Case

Fixed, unchanging strings

Single-threaded string manipulation

Multi-threaded string manipulation

Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.