Skip to content
  • StringBuilder & StringBuffer · Java

StringBuilder & StringBuffer

LearnViaNotes @learnvianotes· 2 min read

A mutable character buffer. Use StringBuilder whenever you build a string piece by piece — especially in loops.

StringStringBuilderStringBuffer
Mutable?NoYesYes
Thread-safe?Yes (immutable)NoYes (synchronized methods)
SpeedSlow for repeated editsFastestSlower (locking)
Since1.01.51.0
Use whenValues that don't changeBuilding strings in one thread (almost always)Legacy code; shared buffer across threads (rare)

Core operations

Java
public class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Java");
        sb.append(' ').append(21).append("!");     // chaining — each call returns this
        System.out.println(sb);
        sb.insert(0, ">> ");
        System.out.println(sb);
        sb.replace(3, 7, "JAVA");
        System.out.println(sb);
        sb.deleteCharAt(sb.length() - 1).delete(0, 3);
        System.out.println(sb);
        sb.setCharAt(0, 'j');
        System.out.println(sb.reverse());
        System.out.println(sb.length() + " " + sb.indexOf("AV") + " " + sb.charAt(0));
        String result = sb.toString();              // freeze into an immutable String
        System.out.println(result);
    }
}
Output
Java 21!
>> Java 21!
>> JAVA 21!
JAVA 21
12 AVAj
7 3 1
12 AVAj

Capacity

A StringBuilder has a capacity (default 16). When full, it grows to roughly (old * 2) + 2 and copies — amortised O(1) per append. Pre-size with new StringBuilder(expectedLength) when you know it.

Java
public class Main {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        System.out.println(sb.capacity());
        sb.append("12345678901234567");            // 17 chars > 16
        System.out.println(sb.capacity());
        System.out.println(new StringBuilder("abc").capacity());   // 16 + 3
    }
}
Output
16
34
19

Pitfalls

Java
public class Main {
    public static void main(String[] args) {
        StringBuilder a = new StringBuilder("x");
        StringBuilder b = new StringBuilder("x");
        System.out.println(a.equals(b));                          // identity! not overridden
        System.out.println(a.toString().equals(b.toString()));
        System.out.println(a.compareTo(b) == 0);                  // Java 11+: content compare

        StringBuilder c = new StringBuilder('A');                  // char → int capacity 65!
        System.out.println("[" + c + "] " + c.capacity());
    }
}
Output
false
true
true
[] 65

Why is StringBuilder faster than String concatenation in a loop?

StringBuilder vs StringBuffer?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report