0Pricing
Java Academy · Lesson

StringBuilder vs StringBuffer

Thread safety trade-offs.

Two Mutable Classes

Java has two mutable string classes: StringBuilder and StringBuffer. Their APIs are nearly identical, but they differ in one crucial way: thread safety.

Same API

Both classes offer append, insert, delete, reverse, and the rest. Code using one can usually switch to the other by changing only the type name.

public class Main {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
        sb.append("Hello").append(", ").append("World");
        System.out.println(sb);
    }
}

All lessons in this course

  1. Why Strings Are Immutable
  2. Building Strings with StringBuilder
  3. StringBuilder Methods
  4. StringBuilder vs StringBuffer
← Back to Java Academy