String StringBuffer和StringBuilder

1 Variability

String objects cannot be changed once created, because strings are placed in the constant pool.

Strings in StringBuffer and StringBuilder are mutable because strings are placed in character arrays.

2 thread safety

StringBuilder is not thread safe, StringBuffer is thread safe.

3 StringBuilder and StringBuffer are both subclasses of AbstractStringBuilder

4 Processing speed

String is the slowest, StringBuilder is the fastest, and StringBuffer is the middle.

Because String is immutable, if you add it to append, you actually need to create a new object, which increases the burden of gc.

StringBuffer is locked, and there is more overhead.

5 Applicable situations

String is suitable for the case where the string does not change.

StringBuilder single-threaded, string volatile situation.

StringBuffer is multi-threaded, and the string is volatile.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326619480&siteId=291194637