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.
Saved automatically
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.
Related Questions in Java
Advanced Java Interview Questions and Answers
Advanced level • 841 views
Java Web development interview questions
Advanced level • 663 views
Java Interview questions
Advanced level • 629 views
Java questions for Newbies
Intermediate level • 573 views
Interview questions on EJB
Advanced level • 454 views