You are on page 1of 2

Difference

String vs StringBuffer vs StringBuilder

String is one of the most widely used classes in Java. StringBuffer and
StringBuilder classes provide methods to manipulate strings. We will look
into the difference between StringBuffer and StringBuilder. StringBuffer vs
StringBuilder is a popular Java interview question.

String vs StringBuffer
String String Buffer
Is immutable Is mutable
Is slow and consumes more Is fast and consumes less memory
memory when you concat too when you concat strings
many strings because every time it
creates new instance.
Overrides the equals() method of Doesn’t override the equals()
object classes. So you can compare method of object classes.
the contents of two strings by
equals() method.
Non synchronized. Synchronized.

StringBuffer vs StringBuilder
String Buffer String Builder
Synchronized. Non synchronized.
i.e. i.e.
thread safe . It means two threads not thread safe . It means two
can’t call the methods of threads can call the methods of
StringBuffer simultaneously. StringBuilder simultaneously.
Is less efficient than StringBuilder Is less efficient than StringBuffer
String , StringBuffer , StringBuilder
String String Buffer String Builder
 If our content is  If our content is  If our content is
fixed and not not fixed as well not fixed
changing as thread safety ,changed
frequently then is required then frequently and
we should go for we should go for thread safety is
String String Buffer . not required
then we should
go for String
Builder .

Assmaa Ali
IT 3

You might also like