Verified Jobs Live
Java Beginner Level ~3 min read

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

Quick Revision Summary

String is immutable. StringBuffer is mutable and thread-safe (synchronized). StringBuilder is mutable and fast (non-thread-safe).

Interactive Practice Mode
Formulate your thoughts or take notes before revealing the model solution.
Model Answer & Deep-Dive Explanation

String: Immutable object stored in the String Constant Pool. Any modification (e.g. concatenation) instantiates a new String object on the heap, resulting in memory fragmentation during frequent string manipulation loops.

StringBuffer: Mutable sequence of characters where methods are synchronized. It is completely thread-safe but incurs synchronization locking overhead.

StringBuilder: Mutable sequence introduced in Java 5. Identical API to StringBuffer but non-synchronized. It offers maximum execution speed in single-threaded environments and is the recommended default choice.

Interviewer Evaluation Points & Scoring Rubric

  • 1 1. Explains String immutability and memory cost. 2. Identifies synchronization difference. 3. Chooses StringBuilder for single-threaded performance.