StringBuilder is a mutable sequence of characters available in the java.lang package. Unlike String, it allows modifications without replacing the existing object.
- Supports operations such as appending, inserting, deleting, and replacing characters.
- Automatically expands its internal capacity when required.
- Does not provide synchronization, making it suitable for single-threaded string manipulation.
Example: Modifying a StringBuilder object using append().
public class Geeks {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("GeeksforGeeks");
System.out.println("Initial StringBuilder: " + sb);
// Append text to the existing sequence
sb.append(" is awesome!");
System.out.println("After append: " + sb);
}
}
Output
Initial StringBuilder: GeeksforGeeks After append: GeeksforGeeks is awesome!
Explanation: The append() method adds text to the end of the existing character sequence without creating another StringBuilder object.
Declaration
StringBuilder sb = new StringBuilder("Initial String");
Hierarchy of StringBuilder
StringBuilder is a final class that extends AbstractStringBuilder and implements the Serializable, CharSequence, and Comparable<StringBuilder> interfaces.

StringBuilder Constructors
The StringBuilder class provides four constructors for initializing objects with different contents and capacities.
- StringBuilder(): Creates an empty builder with an initial capacity of 16.
- StringBuilder(int capacity): Creates an empty builder with the specified initial capacity.
- StringBuilder(String str): Initializes the builder with the specified string.
- StringBuilder(CharSequence cs): Initializes the builder with the specified character sequence.
Example: Initializing StringBuilder using different constructors.
public class Geeks{
public static void main(String[] args){
// Default constructor
StringBuilder sb1 = new StringBuilder();
System.out.println("Default capacity: "+ sb1.capacity());
// Specified capacity
StringBuilder sb2 = new StringBuilder(50);
System.out.println("Custom capacity: "+ sb2.capacity());
// Initialize with String
StringBuilder sb3 = new StringBuilder("Geeks");
System.out.println("String: " + sb3);
// Initialize with CharSequence
CharSequence cs = "Java";
StringBuilder sb4 = new StringBuilder(cs);
System.out.println("CharSequence: " + sb4);
}
}
Output
Default capacity: 16 Custom capacity: 50 String: Geeks CharSequence: Java
Explanation: The first two constructors initialize empty builders with different capacities, while the remaining constructors initialize builders with existing character sequences.
Commonly Used StringBuilder Methods
StringBuilder provides methods for modifying, accessing, and searching character sequences.
Example: Performing different string manipulation operations.
public class Geeks{
public static void main(String[] args){
StringBuilder sb = new StringBuilder("Hello");
// Append characters
sb.append(" World");
System.out.println("After append: " + sb);
// Insert characters
sb.insert(5, " Java");
System.out.println("After insert: " + sb);
// Replace characters
sb.replace(0, 5, "Hi");
System.out.println("After replace: " + sb);
// Delete characters
sb.delete(2, 7);
System.out.println("After delete: " + sb);
// Reverse characters
sb.reverse();
System.out.println("After reverse: " + sb);
// Get length and capacity
System.out.println("Length: " + sb.length());
System.out.println("Capacity: " + sb.capacity());
}
}
Output
After append: Hello World After insert: Hello Java World After replace: Hi Java World After delete: Hi World After reverse: dlroW iH Length: 8 Capacity: 21
Explanation:
- append() adds characters at the end of the sequence.
- insert() adds characters at a specified position.
- replace() and delete() modify characters within a specified range.
- reverse() reverses the character sequence.
- length() returns the current length, while capacity() returns the available storage capacity.
Note: For methods that accept start and end indices, such as delete() and substring(), the start index is inclusive and the end index is exclusive.
Important StringBuilder Methods
Method | Description |
|---|---|
Adds a value to the end of the sequence. | |
| Inserts a value at a specified position. |
Replaces characters within a specified range. | |
Removes characters within a specified range. | |
Removes the character at a specified index. | |
Reverses the character sequence. | |
Returns the length in UTF-16 code units. | |
Returns the current capacity. | |
Returns the character at a specified index. | |
Updates the character at a specified index. | |
Returns a String containing the specified character range. | |
Ensures the capacity meets the specified minimum. | |
| Returns the index of the first occurrence of a string, or -1 if not found. |
Returns the index of the last occurrence of a string, or -1 if not found. | |
Returns a String representation of the character sequence. |
Advantages of StringBuilder
- Reduces intermediate String object creation during repeated concatenation.
- Provides flexible character manipulation without replacing the builder object.
- Allows developers to specify an initial capacity to reduce internal storage reallocations.
Limitations of StringBuilder
- Requires external synchronization when shared across threads performing concurrent modifications.
- Excessive initial capacity may result in unnecessary memory consumption.
Note: StringBuilder is generally preferred for repeated string modifications, while StringBuffer provides synchronized operations and String represents immutable text.