In a multi-threaded environment, which class (StringBuffer or StringBuilder) would you primarily use and why?

  • Both StringBuffer and StringBuilder can be used interchangeably
  • Neither StringBuffer nor StringBuilder should be used in a multi-threaded environment
  • StringBuffer
  • StringBuilder
In a multi-threaded environment, StringBuilder is primarily used due to its better performance. Unlike StringBuffer, StringBuilder is not synchronized, which makes it more efficient when thread safety is not a concern. StringBuffer is synchronized and is used when thread safety is required. Using both interchangeably may lead to synchronization overhead.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *