String
|
StringBuffer
|
StringBuilder
|
|
Storage Area
|
Constant String Pool
|
Heap
|
Heap
|
Modifiable
|
No (immutable)
|
Yes (mutable)
|
Yes (mutable)
|
Thread Safe
|
Yes
Every immutable object in Java is Thread safe.
|
Yes
All method synchronized.
|
No
|
Performance
|
Fast
|
Very slow
All method synchronized.
|
Fast
|
Example
|
String demo = "hello " ;
demo = “bye”;
"hello" string still exists in string constant pool.
new "Bye" string is created in constant pool and
referenced by the demo variable.
|
StringBuffer demo = new StringBuffer("Hello");
Object stored in heap and its value can be changed.
demo=new StringBuffer("Bye");
|
StringBuilder demo = new StringBuilder ("Hello");
Object stored in heap and its value can be changed.
demo=new StringBuilder ("Bye");
|
Wednesday, 29 July 2015
String vs StringBuffer vs StringBuilder
Labels:
Core Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment