StringBuilder vs StringBuffer
Thread safety trade-offs.
StringBuilder vs StringBuffer is a free Java Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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);
}
}StringBuffer Is Synchronized
StringBuffer methods are synchronized, meaning multiple threads can safely modify the same instance. This safety has a small performance cost.
public class Main {
public static void main(String[] args) {
StringBuffer buffer = new StringBuffer("thread-safe");
buffer.append("!");
System.out.println(buffer);
}
}StringBuilder Is Not Synchronized
StringBuilder skips synchronization, making it faster. The trade-off is that it is not safe to share one instance across threads without external coordination.
public class Main {
public static void main(String[] args) {
StringBuilder builder = new StringBuilder("fast");
builder.append("!");
System.out.println(builder);
}
}When to Use StringBuilder
Use StringBuilder in the common case: building strings inside a single method or thread. This is the default choice for almost all everyday code.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5; i++) {
sb.append("#");
}
System.out.println(sb);
}
}When to Use StringBuffer
Reach for StringBuffer only when the same builder instance is genuinely shared and mutated by multiple threads. This is rare in practice.
public class Main {
public static void main(String[] args) {
StringBuffer shared = new StringBuffer("log: ");
shared.append("event A; ");
shared.append("event B");
System.out.println(shared);
}
}Local Variables Are Not Shared
A builder declared and used only inside one method is never visible to other threads, so StringBuilder is always correct there. The lack of synchronization cannot cause a problem.
public class Main {
static String build() {
StringBuilder sb = new StringBuilder();
sb.append("local only");
return sb.toString();
}
public static void main(String[] args) {
System.out.println(build());
}
}Performance Difference
The synchronization overhead in StringBuffer makes it slower in single-threaded use. For tight loops, StringBuilder is the better performer.
public class Main {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100000; i++) {
sb.append('x');
}
System.out.println("Built length: " + sb.length());
}
}History Note
StringBuffer came first, in early Java. StringBuilder was added in Java 5 as the unsynchronized, faster alternative for the common single-threaded case.
Choosing Correctly
Default to StringBuilder. Only switch to StringBuffer when you can prove a single instance is shared across threads. Otherwise you pay for safety you do not need.
public class Main {
public static void main(String[] args) {
StringBuilder result = new StringBuilder();
result.append("Use StringBuilder ").append("by default");
System.out.println(result);
}
}Both Convert to String
Either class produces an immutable String through toString(). The choice between them only affects building, not the final result.
public class Main {
public static void main(String[] args) {
String a = new StringBuilder("one").toString();
String b = new StringBuffer("two").toString();
System.out.println(a + " " + b);
}
}Quick Check
What is the main difference between StringBuilder and StringBuffer?
Recap
You learned the trade-offs:
- Both have nearly identical APIs
StringBufferis synchronized (thread-safe, slower)StringBuilderis unsynchronized (faster)- Default to
StringBuilderfor single-threaded building - Use
StringBufferonly for genuinely shared instances
Frequently asked questions
Is the “StringBuilder vs StringBuffer” lesson free?
Yes — the full text of “StringBuilder vs StringBuffer” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “StringBuilder vs StringBuffer”?
Thread safety trade-offs. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “StringBuilder vs StringBuffer” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Strings Are Immutable
- Building Strings with StringBuilder
- StringBuilder Methods
- StringBuilder vs StringBuffer