☕ Java Beginner

What is the difference between String, StringBuilder, and StringBuffer?

Why Interviewers Ask This

This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Java topics. It also reveals how well you can explain technical ideas to non-experts.

Answer

String is immutable — every modification creates a new object, making it inefficient for repeated string building. StringBuilder is mutable and allows in-place modification without creating new objects — ideal for building strings in loops. It is not thread-safe but faster because it has no synchronization overhead. StringBuffer is identical to StringBuilder but thread-safe (all methods are synchronized) — use it only in multi-threaded contexts where multiple threads modify the same buffer. For single-threaded string building, always prefer StringBuilder over StringBuffer for performance.

Common Mistake

A common mistake is memorizing definitions without understanding implications. When asked this question, go one level deeper — explain what happens when this concept is misused or ignored.