Friday, 22 April 2016

String vs. StringBuffer vs. StringBuilder


String is immutable 
whereas StringBuffer and StringBuilder can change their values.

The only difference between StringBuffer and StringBuilder is that StringBuilder is not synchronized whereas StringBuffer is synchronized.
so, when the application needs to be run only in a single thread then it is better to use StringBuilder.
StringBuilder is more efficient than StringBuffer.


Criteria to choose among String, StringBuffer and StringBuilder : 

  • String : If your text is not going to change as String object is immutable.
  • StringBuilder : If your text can change and will only be accessed from a single thread, as StringBuilder is not synchronized.
  • StringBuffer : If your text can change and will be accessed from multiple threads, as StringBuffer is synchronized.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.