Main StringBuilder Methods in Java
Fast recall
Quick revision
Core answer
StringBuilder operations are append, insert, delete, deleteCharAt, replace, setCharAt, reverse, charAt, length, and toString. Range ends are exclusive. Mutating methods return the same builder for chaining. A new builder has capacity 16 unless a capacity or initial string is supplied; capacity growth is managed by the implementation, so pre-size only when a useful estimate is available.Whether you know the practical StringBuilder API beyond just append() and toString(), especially for coding problems.
Only knowing append() and toString() — in coding interviews StringBuilder.reverse() and deleteCharAt() come up frequently.
Mention that append() is overloaded for all Java types (int, double, char, boolean, Object) — you never need to convert to String first. Also mention that StringBuilder has an initial default capacity of 16 characters and doubles when full.
Interview-ready guide
Interview answer
Most people stop at append() and toString(), but the methods in between are the ones that win coding rounds. I group them four ways.
To add: append() puts content at the end, insert(index, value) drops it at a position. To remove: delete(start, end) cuts a range, deleteCharAt(index) cuts one character. To edit in place: replace(start, end, str) swaps a range, setCharAt(index, ch) overwrites one char, and reverse() flips the whole buffer — the classic move for palindrome checks. To read: charAt(), length(), and indexOf().
Two things stand out. First, append() is overloaded for every Java type — int, double, char, boolean, any Object — so you never convert to a String yourself. Second, all the range methods use the same inclusive-start, exclusive-end rule as substring(), so delete(0, 3) drops three characters.
One detail that shows you've actually used it: a fresh builder starts with capacity 16 by default and grows its capacity as needed, copying the array each time. If you know you're building something large, pass the size into the constructor and skip those copies. And since every mutator returns this, you chain the calls and hit toString() once at the end.
Main StringBuilder Methods in Java
Direct Answer
Key StringBuilder operations are append, insert, delete, deleteCharAt, replace, setCharAt, reverse, charAt, length, and toString. Range ends are exclusive. Mutating methods return the same builder for chaining. A new builder has capacity 16 unless a capacity or initial string is supplied; capacity growth is managed by the implementation, so pre-size only when a useful estimate is available.
Code Example
public class StringBuilderMethods {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello");
// append() — add to the end, overloaded for all types
sb.append(" World"); // Hello World
sb.append("!"); // Hello World!
sb.append(42); // Hello World!42 (no conversion needed)
// insert(index, value) — add at a specific position
sb.insert(5, ","); // Hello, World!42
// delete(start, end) — end is exclusive
sb.delete(13, 15); // Hello, World! (removed "42")
// replace(start, end, str) — swap a range with new text
sb.replace(7, 12, "Java"); // Hello, Java!
// reverse() — flips all characters
StringBuilder rev = new StringBuilder("abcde");
rev.reverse();
System.out.println(rev); // edcba
// charAt() and setCharAt()
System.out.println(sb.charAt(0)); // H
sb.setCharAt(0, 'h'); // hello, Java!
// deleteCharAt() — remove single character
sb.deleteCharAt(sb.length() - 1); // hello, Java
// length() and toString()
System.out.println(sb.length()); // 11
String result = sb.toString(); // convert to immutable String
System.out.println(result); // hello, Java
}
}
Common Mistakes & Pitfalls
- Knowing only
append()andtoString()— interview problems lean onreverse(),deleteCharAt(), andsetCharAt(); not knowing them stalls you on otherwise simple questions. - Treating the end index as inclusive —
delete(0, 3)removes indices 0, 1, and 2 — three chars, not four. End is exclusive, exactly likesubstring(). - Confusing
setCharAt()withinsert()—setCharAt(i, c)overwrites the char atiand keeps the length the same;insert(i, c)shifts everything right and grows the length by one. - Expecting a mutator to return a new builder —
append(),reverse(), and friends returnthis, the same object. The original is mutated in place, so there is no separate "old" copy to fall back on.
Check your understanding
Answer these without looking back at the article.
- How do you reverse a String in Java using StringBuilder?
- How does StringBuilder manage its internal capacity and when does it resize?
- What is the difference between delete() and deleteCharAt() in StringBuilder?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.