String,StringBuffer和StringBuilder

String, StringBuffer and StringBuilder respectively, should be used under what circumstances?
String is a Java string class, which is essentially a Char type of storage, but in addition to hash attribute, other attributes are declared for final type.

Because this operation which generated invariance, brings the following advantages:

  1. String data storage because often use to do the hash function, it is possible to set the final hash value is unchanged.

  2. Involves the base Flyweight, if not set to the final type, then Flyweight mechanism can not be used.

  3. String parameter invariance, inherently bring thread-safe, you can use the safe in multiple threads, without having to add thread lock.


However, since the above convenience only sacrifice some things, but as an adult I have to be sure to select, and then produced a StringBuffer and StringBuilder.

StringBuilder StringBuffer and generates a large amount that the string concatenation to solving many problems intermediate objects, classes provided.

But why have two of it?

Still make threading issues, StringBuilder plus thread locks can be used safely in the case of multi-threaded and single-threaded case we only use stringBuffer, so it does not lock the thread higher performance.

Guess you like

Origin www.cnblogs.com/A-FM/p/10928914.html