概要
String、StringBuffer、StringBuilder是常用的字符序列,從源碼上對比下,三者的區別
類結構
String
StringBuffer
StringBuilder
-
都實現了
interface CharSequence
,interface Comparable<T>
,interface Serializable
-
StringBuilder
,StringBuffer
繼承了abstract class AbstractStringBuilder
CharSequence
: A CharSequence is a readable sequence of char values. This interface provides uniform, read-only access to many different kinds of char sequences.
Comparable
: This interface imposes a total ordering on the objects of each class that implements it.This ordering is referred to as the class's NATURAL ORDERING, and the class's compareTo method is referred to as its natural comparison method
Serializable
: Serializability of a class is enabled by the class implementing the java.io.Serializable interface.
AbstractStringBuilder
: A MUTABLE SEQUENCE of characters. Implements a modifiable string. At any point in time it contains some particular sequence of characters, but the length and content of the sequence CAN BE CHANGED through certain method calls.
Appendable
:An object to which char sequences and values can be appended.
數據結構
String
final
型byte數組,不可修改性的源頭。
StringBuffer、StringBuilder
在java.lang.AbstractStringBuilder
中:
/**
* The value is used for character storage.
*/
byte[] value;
通過繼承java.lang.Appendable
支持修改
設計目標
String
Strings are constant; their values cannot be changed after they are created.
String buffers support mutable strings.Because String objects are immutable they can be shared
StringBuffer
* A thread-safe, mutable sequence of characters.
* A string buffer is like a {@link String}, but can be modified. At any
* point in time it contains some particular sequence of characters, but
* the length and content of the sequence can be changed through certain
* method calls.
* <p>
* String buffers are safe for use by multiple threads. The methods
* are synchronized where necessary so that all the operations on any
* particular instance behave as if they occur in some serial order
* that is consistent with the order of the method calls made by each of
* the individual threads involved.
StringBuilder
* A mutable sequence of characters. This class provides an API compatible
* with {@code StringBuffer}, but with no guarantee of synchronization.
* This class is designed for use as a drop-in replacement for
* {@code StringBuffer} in places where the string buffer was being
* used by a single thread (as is generally the case). Where possible,
* it is recommended that this class be used in preference to
* {@code StringBuffer} as it will be faster under most implementations.
無參構造函數
String
/**
* Initializes a newly created {@code String} object so that it represents
* an empty character sequence. Note that use of this constructor is
* unnecessary since Strings are immutable.
*/
public String() {
this.value = "".value;
this.coder = "".coder;
}
StringBuffer、StringBuilder
在java.lang.AbstractStringBuilder
中:
/**
* Creates an AbstractStringBuilder of the specified capacity.
*/
AbstractStringBuilder(int capacity) {
if (COMPACT_STRINGS) {
value = new byte[capacity];
coder = LATIN1;
} else {
value = StringUTF16.newBytesFor(capacity);
coder = UTF16;
}
}
/**
* Constructs a string buffer with no characters in it and an
* initial capacity of 16 characters.
*/
@HotSpotIntrinsicCandidate
public StringBuffer() {
super(16);
}
/**
* Constructs a string builder with no characters in it and an
* initial capacity of 16 characters.
*/
@HotSpotIntrinsicCandidate
public StringBuilder() {
super(16);
}
默認byte[]初始化長度時16,調用append方法時,長度不夠,會擴容,進行數組複製。
已知內容的情況下,可以通過指定長度,來避免擴容、減少數組複製。
一般情況下,可以不用考慮這麼多,性能要求嚴格的情況下,需要考慮減少數組複製。
Arrays.copyOf
底層是java.lang.System#arraycopy
,arraycopy
在JVM層面,會有更高效的方法替代。
總結
- String 初始化後不可修改,StringBuilder、StringBuffer支持修改。
- 操作少量的數據或者常量使用 String
- 單線程操作字符串緩衝區下操作大量數據,使用StringBuilder
- 多線程操作字符串緩衝區下操作大量數據,使用StringBuffer
- 性能嚴格要求的場景下,StringBuilder、StringBuffer可以通過指定初始化容量,減少數組複製