StringBuilder
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 bufferstr
(Optional): Initial string to be stored in the StringBuilderseq
(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 StringBuilderStringBuilder emptyBuilder = new StringBuilder();System.out.println("Empty builder capacity: " + emptyBuilder.capacity()); // Default capacity is 16// Create StringBuilder with specific capacityStringBuilder capacityBuilder = new StringBuilder(50);System.out.println("Capacity builder capacity: " + capacityBuilder.capacity());// Create StringBuilder with initial contentStringBuilder contentBuilder = new StringBuilder("Hello");System.out.println("Content builder: " + contentBuilder);// Convert StringBuilder to StringString result = contentBuilder.toString();System.out.println("Converted to String: " + result);}}
This code generates the output as follows:
Empty builder capacity: 16Capacity builder capacity: 50Content builder: HelloConverted 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 endsb.append(" is");sb.append(" awesome");System.out.println("After append: " + sb);// Insert content at a specific positionsb.insert(5, "programming ");System.out.println("After insert: " + sb);// Replace contentsb.replace(0, 4, "Python");System.out.println("After replace: " + sb);// Delete contentsb.delete(7, 19);System.out.println("After delete: " + sb);// Reverse the contentsb.reverse();System.out.println("After reverse: " + sb);// Reset by clearing and adding new contentsb.setLength(0); // Clear the StringBuildersb.append("Fresh start");System.out.println("After reset: " + sb);}}
The output generated by the code will look like:
Original: JavaAfter append: Java is awesomeAfter insert: Java programming is awesomeAfter replace: Python programming is awesomeAfter delete: Python is awesomeAfter reverse: emosewa si nohtyPAfter 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 lengthSystem.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 millisecondsTime taken with StringBuilder: 5 millisecondsString length: 100000StringBuilder 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 spacesb.append("World"); // Append a stringsb.append('!'); // Append a charactersb.append(2023); // Append a numberString 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.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours