StringBuilder

StevenSwiniarski's avatar
Published Aug 22, 2022Updated Apr 24, 2025
Contribute to Docs

In Java, the StringBuilder class is a part of the java.lang package that creates a mutable sequence of characters. Unlike String objects, which are immutable, StringBuilder allows modifying character sequences without creating new objects for each operation. This makes StringBuilder much more efficient when performing multiple string manipulations, especially within loops or when building strings incrementally.

StringBuilder is commonly used in Java applications for tasks such as constructing SQL queries, assembling JSON or XML data, formatting large text outputs, or any scenario where strings need to be concatenated or modified repeatedly. Its ability to modify strings in-place significantly reduces memory overhead and improves performance in string-intensive operations.

Syntax

To use StringBuilder in Java:

// Different ways to create a StringBuilder
StringBuilder sb = new StringBuilder();                 // Creates an empty StringBuilder with default capacity (16)
StringBuilder sb = new StringBuilder(int capacity);     // Creates a StringBuilder with the capacity specified
StringBuilder sb = new StringBuilder(String str);       // Creates StringBuilder with initial content
StringBuilder sb = new StringBuilder(CharSequence seq); // Creates StringBuilder with CharSequence content

Parameters:

  • capacity(Optional): Initial capacity of the StringBuilder buffer
  • str(Optional): Initial string to be stored in the StringBuilder
  • seq(Optional): Initial character sequence to be stored in the StringBuilder

Return value:

StringBuilder returns a reference to a mutable sequence of characters which can be converted to a String using the toString() method.

StringBuilder vs StringBuffer

StringBuffer is another mutable sequence class in Java that is similar to StringBuilder. Both classes provide methods for modifying character sequences, but they differ in significant ways:

Feature StringBuilder StringBuffer
Thread Safety Not thread-safe Thread-safe (synchronized)
Performance Faster due to no synchronization Slightly slower due to synchronization overhead
Usage Scenario Single-threaded environments Multi-threaded environments
Introduction Java 1.5 Java 1.0
Memory Efficiency More efficient for single thread Same implementation with thread safety mechanism
API Methods Identical to StringBuffer Identical to StringBuilder

Example 1: Creating a StringBuilder

This example demonstrates the basic creation and usage of StringBuilder:

public class StringBuilderCreationExample {
public static void main(String[] args) {
// Create an empty StringBuilder
StringBuilder emptyBuilder = new StringBuilder();
System.out.println("Empty builder capacity: " + emptyBuilder.capacity()); // Default capacity is 16
// Create StringBuilder with specific capacity
StringBuilder capacityBuilder = new StringBuilder(50);
System.out.println("Capacity builder capacity: " + capacityBuilder.capacity());
// Create StringBuilder with initial content
StringBuilder contentBuilder = new StringBuilder("Hello");
System.out.println("Content builder: " + contentBuilder);
// Convert StringBuilder to String
String result = contentBuilder.toString();
System.out.println("Converted to String: " + result);
}
}

This code generates the output as follows:

Empty builder capacity: 16
Capacity builder capacity: 50
Content builder: Hello
Converted to String: Hello

This example shows different ways to create a StringBuilder and illustrates that the default capacity is 16 characters if not specified. It also demonstrates how to convert a StringBuilder back to a regular String using the .toString() method.

Example 2: Modifying StringBuilder Content

This example shows various methods for manipulating the content of a StringBuilder:

public class StringBuilderModificationExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Java");
System.out.println("Original: " + sb);
// Append content to the end
sb.append(" is");
sb.append(" awesome");
System.out.println("After append: " + sb);
// Insert content at a specific position
sb.insert(5, "programming ");
System.out.println("After insert: " + sb);
// Replace content
sb.replace(0, 4, "Python");
System.out.println("After replace: " + sb);
// Delete content
sb.delete(7, 19);
System.out.println("After delete: " + sb);
// Reverse the content
sb.reverse();
System.out.println("After reverse: " + sb);
// Reset by clearing and adding new content
sb.setLength(0); // Clear the StringBuilder
sb.append("Fresh start");
System.out.println("After reset: " + sb);
}
}

The output generated by the code will look like:

Original: Java
After append: Java is awesome
After insert: Java programming is awesome
After replace: Python programming is awesome
After delete: Python is awesome
After reverse: emosewa si nohtyP
After reset: Fresh start

This example demonstrates the main operations you can perform with StringBuilder including append, insert, replace, delete, and reverse methods. It also shows how to reset a StringBuilder by setting its length to zero, which is more efficient than creating a new instance.

Example 3: Performance Optimization with StringBuilder

This example demonstrates how StringBuilder improves performance when concatenating strings in a loop compared to using the + operator with String:

public class StringBuilderPerformanceExample {
public static void main(String[] args) {
// Using String concatenation (inefficient)
long startTime = System.currentTimeMillis();
String regularString = "";
for (int i = 0; i < 100000; i++) {
regularString += "a";
}
long endTime = System.currentTimeMillis();
System.out.println("Time taken with String concatenation: " +
(endTime - startTime) + " milliseconds");
// Using StringBuilder (efficient)
startTime = System.currentTimeMillis();
StringBuilder efficientBuilder = new StringBuilder();
for (int i = 0; i < 100000; i++) {
efficientBuilder.append("a");
}
String builderResult = efficientBuilder.toString();
endTime = System.currentTimeMillis();
System.out.println("Time taken with StringBuilder: " +
(endTime - startTime) + " milliseconds");
// Verify both strings have the same length
System.out.println("String length: " + regularString.length());
System.out.println("StringBuilder result length: " + builderResult.length());
}
}

The output of this code will look like (times will vary based on machine):

Time taken with String concatenation: 4367 milliseconds
Time taken with StringBuilder: 5 milliseconds
String length: 100000
StringBuilder result length: 100000

This example clearly demonstrates why StringBuilder should be used for string concatenation in loops. The string concatenation approach creates a new string object on each iteration, leading to significantly worse performance compared to StringBuilder, which modifies the same buffer in-place.

Frequently Asked Questions

1. When should I use StringBuilder in Java?

Use StringBuilder when you need to perform multiple string manipulations, especially in loops or when constructing strings incrementally. It’s ideal for scenarios where you’re building strings dynamically, such as in response generation, SQL query building, or when formatting complex output.

2. Why is StringBuilder better than string in Java?

StringBuilder is better than string for frequent modifications because string objects are immutable, meaning each modification creates a new string instance. StringBuilder modifies the same object in-place, resulting in significantly better performance and less memory usage when performing multiple string operations.

3. What is the size limit of StringBuilder in Java?

The theoretical maximum size of a StringBuilder is Integer.MAX_VALUE (approximately 2.14 billion characters). However, practical limits depend on available heap memory. StringBuilder dynamically expands its capacity as needed, growing by (oldCapacity * 2) + 2 when required.

4. What are the disadvantages of StringBuilder in Java?

The main disadvantages of StringBuilder include:

  • It’s not thread-safe, requiring external synchronization in multithreaded environments
  • It can be less readable than simple string concatenation for basic operations
  • It may consume more initial memory than necessary if the capacity is set too high
  • Converting to String requires an additional step using toString().

5. How to concatenate in StringBuilder?

To concatenate strings using StringBuilder, use the .append() method. For example:

StringBuilder sb = new StringBuilder("Hello");
sb.append(" "); // Append a space
sb.append("World"); // Append a string
sb.append('!'); // Append a character
sb.append(2023); // Append a number
String result = sb.toString(); // "Hello World!2023"

The .append() method can accept various data types including String, char, int, long, boolean, and other objects, which are automatically converted to their string representation.

StringBuilder

.append()
Appends the string representation of its argument.
.capacity()
Returns the current space available for characters in the StringBuilder.
.delete()
Removes a substring from the contents of a StringBuilder and returns the modified object.
.deleteCharAt()
Removes the character at the specified index from the contents of a StringBuilder and returns the modified object.
.indexOf()
Returns the index of the first occurrence of a substring in the StringBuilder or -1 if none are found.
.insert()
Places a sequence of characters into a StringBuilder and returns a reference to the object.
.lastIndexOf()
Returns the index of the last (rightmost) occurrence of a substring in the StringBuilder or -1 if no substring is found.
.length()
Returns the number of characters currently in the StringBuilder.
.replace()
Switches a substring in a StringBuilder with a specified String and returns the modified object.
.reverse()
Returns a modified StringBuilder object with its character sequence rearranged in the opposite order.
.substring()
Returns a String object representing a substring of the character sequence currently in a given StringBuilder.
.toString()
Returns a String representation of the character sequence currently in a given StringBuilder.

All contributors

Contribute to Docs

Learn Java on Codecademy