Java Search Engine

freerice.com

Friday, March 28, 2008

Difference between String, StringBuffer, StringBuilder


String: String is immutable. You can't change a string object once after you created it. The methods in String class which appear to be changing string object are actually return a new object.

StringBuffer: StringBuffer is mutable. You can change it without creating new object. Use it when you have to modify your string a lot.

StringBuilder: StringBuilder is introduced in Java SE 5.0. It is same as StringBuffer in functionality. But it doesn't have its methods synchronized, whereas StringBuffer has synchronized methods. When you don't need thread safety for your strings use StringBuilder, it will be little fast as you don't need to get locks unnecessarily. Most of the time you don't need synchronized methods.

No comments:

Ratings by outbrain