CodeToConcept
About

java

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

Sample Question

Think it through, then check the tutorial.

CodeToConcept

Quickly learn through simple, structured, and practical coding tutorials. Build your confidence from step zero to real-world experience.

© 2026 CodeToConcept. All rights reserved.

Quick Links

AboutTutorials

Tutorials

JavaHTMLCSS

Support

Privacy PolicyTerms of ServiceContact
GitHubTwitterYouTube

StringBuffer and StringBuilder in Java

Introduction

StringBuilder and StringBuffer hold text that can be edited after creation. Both support operations such as appending a name or replacing a label. Their important difference is synchronization, not the text they can represent.

Why Do We Need StringBuffer and StringBuilder?

Remember that String is immutable.

For example:

String text = "Hello";
 
text = text + " Java";
text = text + " World";

Each modification produces a new String object.

When many modifications are required, repeatedly creating new String objects can be inefficient.

StringBuilder and StringBuffer provide mutable character sequences.

StringBuilder

StringBuilder is a mutable sequence of characters.

Example

StringBuilder builder = new StringBuilder("Hello");
 
builder.append(" Java");
 
System.out.println(builder);

Output

Hello Java

The existing StringBuilder object is modified.

Modifying a StringBuilder

StringBuilder builder = new StringBuilder("Hello");
 
builder.append(" Java");
builder.insert(5, " World");
builder.replace(0, 5, "Hi");

The object can be changed using its methods.

Common StringBuilder Methods

append()

Adds content to the end.

StringBuilder builder = new StringBuilder("Hello");
 
builder.append(" Java");
 
System.out.println(builder);

Output

Hello Java

insert()

Inserts content at a specified position.

StringBuilder builder = new StringBuilder("Hello");
 
builder.insert(5, " Java");
 
System.out.println(builder);

Output

Hello Java

delete()

Removes characters from a specified range.

StringBuilder builder = new StringBuilder("Hello Java");
 
builder.delete(5, 10);
 
System.out.println(builder);

Output

Hello

replace()

Replaces characters in a specified range.

StringBuilder builder = new StringBuilder("Hello Java");
 
builder.replace(6, 10, "World");
 
System.out.println(builder);

Output

Hello World

reverse()

Reverses the sequence.

StringBuilder builder = new StringBuilder("Java");
 
builder.reverse();
 
System.out.println(builder);

Output

avaJ

StringBuffer

StringBuffer also represents a mutable sequence of characters.

Example

StringBuffer buffer = new StringBuffer("Hello");
 
buffer.append(" Java");
 
System.out.println(buffer);

Output

Hello Java

StringBuffer provides many of the same operations as StringBuilder.

StringBuilder vs StringBuffer

The main difference is synchronization.

StringBuffer provides synchronized operations for shared mutable text. A sequence of separate operations may still need one external lock when the whole sequence must be atomic.

StringBuilder is not synchronized.

Therefore, for ordinary single-threaded string manipulation, StringBuilder is generally preferred because it avoids the synchronization overhead associated with StringBuffer.

FeatureStringBuilderStringBuffer
MutableYesYes
SynchronizedNoYes
Thread-safe for its synchronized operationsNoYes
Typical single-threaded useYesLess common
Performance in ordinary useGenerally fasterGenerally slower due to synchronization

String vs StringBuilder

String text = "Hello";

A String is immutable.

StringBuilder builder = new StringBuilder("Hello");

A StringBuilder is mutable.

For repeated modifications:

StringBuilder builder = new StringBuilder();
 
builder.append("Java");
builder.append(" is");
builder.append(" powerful");

This is often more appropriate than repeatedly concatenating strings.

Converting StringBuilder to String

A StringBuilder can be converted to a String using toString().

StringBuilder builder = new StringBuilder("Hello Java");
 
String text = builder.toString();
 
System.out.println(text);

When Should You Use StringBuilder?

Use StringBuilder when:

  • You need to modify text repeatedly.
  • You are building a String inside a loop.
  • You need append, insert, delete, or replace operations.
  • The code does not require synchronized access to the same buffer.

Example

StringBuilder result = new StringBuilder();
 
for (int i = 1; i <= 5; i++) {
    result.append(i);
}
 
System.out.println(result);

Output

12345

When Should You Use StringBuffer?

Use StringBuffer when you specifically need its synchronized behavior for shared mutable text in concurrent code. For example: StringBuffer buffer = new StringBuffer();

buffer.append("Java"); buffer.append(" Programming");

Key Takeaway

String is immutable, while StringBuilder and StringBuffer are mutable character sequences. StringBuilder is generally preferred for ordinary single-threaded string manipulation, while StringBuffer provides synchronized operations for situations where that behavior is required.

See you in next page....🙂

← String ClassStringBuilder →
← String ClassStringBuilder →