Multiple choice technology

Given this method in a class: 21. public String toString() { 22. StringBuffer buffer = new StringBuffer(); 23. buffer.append('<'); 24. buffer.append(this.name); 25. buffer.append('>'); 26. return buffer.toString(); 27. } Which statement is true?

  1. This code is NOT thread-safe

  2. The programmer can replace StringBuffer with StringBuilder with no other changes

  3. This code will perform poorly. For better performance, the code should be rewritten:return "<" + this.name + ">";

  4. This code will perform well and converting the code to use StringBuilder will not enhance the performance

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

StringBuffer is thread-safe due to synchronization, while StringBuilder is not. Since 'buffer' is a local variable used only within this method, there's no concurrent access - no threads share this instance. Therefore, replacing StringBuffer with StringBuilder removes unnecessary synchronization overhead and improves performance, with identical functionality.