String | String Builder | String Buffer |
---|---|---|
Immutable | Mutable | Mutable |
Synchronised(thread safe) | Non- Synchronised(thread safe) | Synchronised(Not thread safe) |
=> Use String if you need immutability
=> Use String Buffer if you need mutability and thread safety .
=> Use String Builder if you need mutability and don't need thread safety.
String is an immutable class while StringBuilder & StringBuffer are mutable classes. StringBuffer is synchronized while StringBuilder is not.
ReplyDeleteBelow link can be useful to find more differences between String, StringBuffer & StringBuilder
Difference between String, StringBuffer and StringBuilder
http://newtechnobuzzz.blogspot.com/2014/07/difference-between-string-stringbuffer.html