What is the main difference between StringBuffer
and StringBuilder
?
Is there any performance issues when deciding on any one of these?
There are no basic differences between StringBuilder
and StringBuffer
, only a few differences exist between them. In StringBuffer
the methods are synchronized. This means that at a time only one thread can operate on them. If there is more than one thread then the second thread will have to wait for the first one to finish and the third one will have to wait for the first and second one to finish and so on. This makes the process very slow and hence the performance in the case of StringBuffer
is low.
On the other hand, StringBuilder
is not synchronized. This means that at a time multiple threads can operate on the same StringBuilder
object at the same time. This makes the process very fast and hence performance of StringBuilder
is high.