Difference between StringBuffer and StringBuilder in java

In this post, we will see difference between StringBuffer and StringBuilder in java

StringBuffer vs StringBuilder

Parameter
StringBuffer
StringBuilder
Thread-safe
StringBuffer is thread safe. Two threads can not call methods of StringBuffer simultaneously.
StringBuilder is not thread safe, so two threads can call methods of StringBuilder simultaneously.
Performance
It is less performance efficient as it is thread-safe
It is more performance efficient as it is not thread-safe.

Performance comparison between StringBuffer and StringBuilder in single thread environment

When you run above program, you may get below output:
You can go through core java interview questions for more such questions.

Was this post helpful?

Leave a Reply

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