What is the main difference between StringBuffer
and StringBuilder
?
Is there any performance issues when deciding on any one of these?
StringBuilder
and StringBuffer
are almost the same. The difference is that StringBuffer
is synchronized and StringBuilder
is not. Although, StringBuilder
is faster than StringBuffer
, the difference in performance is very little. StringBuilder
is a SUN's replacement of StringBuffer
. It just avoids synchronization from all the public methods. Rather than that, their functionality is the same.
Example of good usage:
If your text is going to change and is used by multiple threads, then it is better to use StringBuffer
. If your text is going to change but is used by a single thread, then use StringBuilder
.