Wednesday, November 30, 2011

String vs String Builder vs String Buffer

String : Immutable
String Builder : Mutable + Thread safe
String Buffer : Mutable + not Thread safe.


Performance wise : string is bad , as for every operation it creates new string object hence immutable. whereas String builder/ String Buffer doesn't create new object very time for string operations.

For ex :

String A = A + "BBC"
then for this a new "A" object will be created. So we are creating those many objects of string class.

When using String Builder/ String Buffer, then operations performed on same object and doesn't create new object of String Buffer/ Builder. Hence mutable.
Therefore performance is good.

##PS : For more refer mutable/immutable

No comments:

Post a Comment