Wednesday, January 11, 2017

Java Difference between String, StringBuffer and String Builder

In this post, I will show you The difference between String, StringBuffer and String Builder. As you will find that there are minor differences between the above classess.




String

String is immutable object. Every immutable object in Java is thread safe, that means can not changed.

StringBuffer

StringBuffer is mutable means one can change the value of the object. The object created through StringBuffer is stored in the heap. StringBuffer has the same methods as StringBuilder, but each method in StringBuffer is synchronized that is StringBuffer is thread safe.

StringBuilder is faster than StringBuffer when calling the same methods of each class.

StringBuffer test = new StringBuffer("Hello");
test = new StringBuffer("Bye");

StringBuilder

StringBuilder is same as the StringBuffer. That is it stores the object in heap and it can also be modified. But the main difference between StringBuffer and StringBuilder is that StringBuilder is also not thread safe. So, StringBuilder is fast as it is not thread safe.


Share:

0 comments:

Post a Comment

Total Pageviews