What are the differences between String, StringBuffer, and StringBuilder in java?

All Java developers use the string in every day’s coding. It is impossible to pass days as a developer without using the Java String class. We also have StringBuffer and StringBuilder classes in Java. Following are some differences of String, StringBuffer and StringBuilder:

# String StringBuffer StringBuilder
1 String is immutable StringBuffer is mutable StringBuilder is mutable
2 String is thread-safe StringBuffer is thread-safe StringBuilder is not thread-safe
3 String is fast StringBuffer is very slow StringBuilder is fast
4 String storage area is constant string pool StringBuffer storage area is heap StringBuilder storage area is heap

We should prefer StringBuilder in all cases where we have only a single thread accessing the object.

Share