StringBuffer and StringBuilder are widely used to manipulate the strings in java.

StringBuffer

StringBuffer basically used for modifying String

If we have String then why we should again learn this new class?


Because String is immutable each modification on String creates new Object while in StringBuffer it is mutable when you modify contents of StringBuffer it does not create new copy instead it modifies the existing copy.


Is it safe that two or more than two threads can operate on StringBuffer?

StringBufer is thread safe even if two or more threads operates on stringbuffer it maintains synchronization between threads.

Now we will see some examples about StringBuffer


StringBuilder


StringBuilder is not thread safe sould not be used in multithreaded environment.

So fatser than StringBuffer.

It is mutable that means it wont create extra copy of data after every modification it will just modify existing object.

Leave a Reply

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