Q/..Why StringBuffer and StringBuilder is mutable and what is difference between them ?
Answer :-
========
========
StringBuffer is synchronized so only one time one thread can access StringBuffer object. SO when you are accessing StringBuffer object no one can change and use this object. So second thread not modify the object of StringBuffer and that is why it's tread safe
You could argue that it was a design mistake to make the StringBuffer class synchronized. It's synchronized to make it thread-safe, which means that if there are multiple threads that try to append data to the same StringBuffer object, then no dobut which is not possible coz it is synchronized
You could argue that it was a design mistake to make the StringBuffer class synchronized. It's synchronized to make it thread-safe, which means that if there are multiple threads that try to append data to the same StringBuffer object, then no dobut which is not possible coz it is synchronized
So to overcome this problem sun people develop another class StringBuilder in 1.5v which is functio wise same with StringBuffer but except synchronized features.
Coz in every time synchronisation is not good . it takes more time to execute and performance wise slow. One benifet only there are no chance of data inconsistency.
Use:-
======
so in simple way
when you want to develop multi thread app with mutability features then prefer StringBuilder
When you want to develop single mode app then better to refer StringBuffer.
======
so in simple way
when you want to develop multi thread app with mutability features then prefer StringBuilder
When you want to develop single mode app then better to refer StringBuffer.
Comments
Post a Comment