Which of the following class among these are synchronized?
-
StringBuilder
-
StringBuffer
-
Both (1) and (2)
-
None of the above
B
Correct answer
Explanation
StringBuffer is synchronized (thread-safe) while StringBuilder is not. StringBuffer's methods are synchronized, making it safe for use in multi-threaded environments, though slower than StringBuilder. StringBuilder was introduced in Java 5 as a non-synchronized alternative for single-threaded contexts.