What is the difference between StringBuffer and StringBuilder?

StringBuffer and StringBuilder both are used for mutability. Both have same methods and constructors but all the methods in StringBuffer are Synchronized whereas, the methods in StringBuilder are not Synchronized.
As the methods in the StringBuffer are synchronized, only one thread can operate on stringbuffer object at a time which gives thread safety, but this reduce performance.Hence wherever performance is more concerned than thread safety, we have to use StringBuilder over StringBuffer.

The difference between StringBuffer and StringBuilder are shown below:
StringBuffer and StringBuilder Differences

No comments:

Post a Comment