Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The StringBuilder class

Reference for Java StringBuilder: append, insert, delete, replace and reverse text efficiently, with capacity, complexity and thread-safety notes.

StringBuilder is a mutable sequence of characters. Where a String can never change, a StringBuilder grows and shrinks in place: you append, insert, delete and replace, then call toString() to get an ordinary string. Building text in a loop with += copies the whole string on every pass, which is quadratic; a StringBuilder appends in amortized constant time.

Most of its methods return the builder itself, so calls can be chained. It is not thread-safe. If several threads must share one, use StringBuffer or, better, give each thread its own builder.

Java
StringBuilder sb = new StringBuilder();
sb.append("a").append(1).append('!');
String s = sb.toString(); // "a1!"

Try it

Java Building and editing text

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Loops, chaining and comparison

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public final class StringBuilder implements Appendable, CharSequence, Comparable<StringBuilder>, java.io.Serializable
Member Description
new StringBuilder() Empty builder with capacity 16. Also (int capacity) and (String or CharSequence initial).
append(x) Appends text, any primitive, a char[] or an object (via String.valueOf). Returns the builder.
insert(int index, x) Inserts at index, shifting the rest to the right.
delete(int start, int end) Removes [start, end); end is clamped to the length.
deleteCharAt(int index) Removes one char.
replace(int start, int end, String s) Replaces [start, end) with s.
reverse() Reverses in place; surrogate pairs stay valid.
setCharAt(int, char), setLength(int) Overwrite one char; truncate or pad with \0.
charAt, length, isEmpty, indexOf, lastIndexOf, substring, capacity Read access and searching.
repeat(CharSequence, int) Appends a sequence several times (Java 21).
toString() Copies the contents into a new String.

Throws: StringIndexOutOfBoundsException (or IndexOutOfBoundsException) for bad indexes; NegativeArraySizeException for a negative capacity.

Usage notes

  • Amortized O(1) append. When full, the backing array grows to roughly double the size. Passing an expected size to the constructor avoids the copies.
  • Inserting or deleting at the front is O(n), because the following characters move. Appending and setCharAt are cheap.
  • A single + expression is fine. The compiler compiles a + b + c efficiently. A builder pays off when concatenating inside loops.
  • sb.append(null) appends the text "null". Appending a null String or Object does the same.
  • No equals() or hashCode(). Two builders with the same text are not equal; do not use them as map keys.
  • Not thread-safe. Do not share one builder across threads without external locking.
  • Return toString(), not the builder, from methods; the builder is a mutable helper, and each toString() call makes a fresh copy.
  • Argument errors in ranges: delete(start, end) and replace() require 0 <= start <= length and start <= end.