javacodex.com
Java Examples
Java Examples
memu home questions

What is the difference between a String , StringBuilder and StringBuffer?

String

String is immutable (once created can not be changed) object. The object created as a String is stored in the Constant String Pool. Every immutable object in Java is thread safe, that implies String is also thread safe. String can not be used by two threads simultaneously. String once assigned can not be 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 the StringBuilder, but each method in StringBuffer is synchronized that is StringBuffer is thread safe.

StringBuilder

StringBuilder is the same as a StringBuffer, but with no guarantee of synchronization.

Summary of Differences

StringStringBufferStringBuilder
Storage AreaConstant String PoolHeapHeap
ModifiableNo (immutable)Yes (mutable)Yes (mutable)
Thread SafeYesYesNo
PerformanceFastVery slowFast

Resources

http://javahungry.blogspot.com/2013/06/difference-between-string-stringbuilder.html



Contact: javacodex@yahoo.com