Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Difference between String and StringBuffer in Java

What is String?

When it comes to Java, a string is considered as an object that defines a sequence of char values. In Java, arrays are unchangeable, likewise strings are unchangeable as well. In any case, if a modification happens in a string, a completely new string is constructed.

What is StringBuffer?

StringBuffer is the equivalent class of the class string. The class StringBuffer delivers more functionality to the other class strings. We can make changes here because the StringBuffer is mutable.

Difference between String and StringBuffer in Java

S. No. String StringBuffer
1 String is immutable. It is mutable.
2 It is slow in terms of executing the concatenation task. It is fast in terms of executing the concatenation task.
3 Here the length of the string class is static. Here the length can be modified whenever required, as it is dynamic in behaviour.
4 It is less efficient. It is more efficient in nature as compared to the string class.
5 String consumes more as compared to the stringbuffer. StringBuffer uses less memory as compared to the string.
5 It utilises a string constant pool to store the values. It prefers heap memory to store the objects.
6 It overrides both equal() and hashcode() techniques of object class. It cannot override equal() and hashcode() methods.

Keep learning and stay tuned to BYJU’S to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2024GATE Admit CardGATE Application FormGATE SyllabusGATE CutoffGATE Previous Year Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*