Thursday, May 10, 2012

What are the differences of String, StringBuffer & StringBuilder in Java?

String is immutable. Thus, manipulating plain Strings creates lots of garbage objects. But, StringBuffer and StringBuilder are mutable.

StringBuffer and  StringBuilder are essentially same except StringBuffer's methods are synchronized.

So, in a code segment which is accessed by multiple threads, StringBuffer should be used, and in all other situations StringBuilder is the most suitable. String can be used if it is guaranteed that its objects won't be manipulated later. e.g. converting to uppercase.

http://javarevisited.blogspot.com/2011/07/string-vs-stringbuffer-vs-stringbuilder.html

No comments: