What are the differences between String and StringBuffer?

Solution:

The differences between the String and StringBuffer is given in the table below.

No. String StringBuffer
1) The String class is immutable. The StringBuffer class is mutable.
2) The String is slow and consumes more memory when you concat too many strings because every time it creates a new instance. The StringBuffer is fast and consumes less memory when you cancat strings.
3) The String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method. The StringBuffer class doesn't override the equals() method of Object class.
0 answers