String vs StringBuffer

String is a final class hence cannot be extended by any class.

Any class which cannot be extended can also be called as immutable class.

StringBuffer is a class which can be extended (mutable).

Every time a new string is appended/added/removed to a string object, it creates a new string object and added to the existing string object.

When a new String object is appended to StringBuffer object, it adds to the same memory of the existing String object already allocated., hence avoiding creating string objects every time..

Hence appending or modifying string data or values using StringBuffer saves more memory.

It reduces overhead of new string objects created every time.



Leave a Reply

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