1. 程式人生 > 其它 >String類的成員變數【原始碼賞析】

String類的成員變數【原始碼賞析】

技術標籤:# String類(魔法傷害)【原始碼賞析】JAVA中的精彩絕倫javastring

String類的成員變數【原始碼賞析】

每一篇文章都屬於作者的勞動成果,尊重原創!尊重知識!從我做起。禁止一切形式的轉載、抄襲、高相似度抄襲、借鑑,謝謝合作。

java原始碼賞析專欄會選取JDK1.8 、JDK11、JDK15的原始碼進行對比,比較每個jdk版本所作的改進,以及每份原始碼的設計理念。

碎碎念

來研究一下String類包含的成員變數

String是java中處理字串的有力“兵器”類,我們需要對這個被反覆使用的類仔細研究,才能榨乾每個工具的價值,才能學會更高超的程式設計技巧。

jdk 1.8

原始碼

在這裡插入圖片描述

/** The value is used for character storage. */
private final char value[];

/** Cache the hash code for the string */
private int hash; // Default to 0

/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -6849794470754667710L;

/**
 * Class String is special cased within the Serialization Stream Protocol.
 *
 * A String instance is written into an ObjectOutputStream according to
 * <a href="{@docRoot}/../platform/serialization/spec/output.html">
 * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
 */
private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[0];

還有常量:

/**
 * A Comparator that orders {@code String} objects as by
 * {@code compareToIgnoreCase}. This comparator is serializable.
 * <p>
 * Note that this Comparator does <em>not</em> take locale into account,
 * and will result in an unsatisfactory ordering for certain locales.
 * The java.text package provides <em>Collators</em> to allow
 * locale-sensitive ordering.
 *
 * @see     java.text.Collator#compare(String, String)
 * @since   1.2
 */
public static final Comparator<String> CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator();

賞析

這裡其決定作用的就是value,型別是char[],字串中的字元序列將其全部儲存於value當中,所以以後大多數String的類方法都將圍繞這個char[]來操作。

可以看到value變數被final修飾,這意味著value一旦被初始化了將永遠無法更改value的值,也就是這個String能存放的字元序列值將不能再修改。

因此對字串平凡的修改,刪除,拼接等操作其實都是在每一次操作時生成一個新的物件,這會造成很高的效能開銷。

hash這個int型的數將會用於儲存String的hash code值。

jdk 11 & jdk 15

原始碼

在這裡插入圖片描述

/**
* The value is used for character storage.
*
* @implNote This field is trusted by the VM, and is a subject to
* constant folding if String instance is constant. Overwriting this
* field after construction will cause problems.
*
* Additionally, it is marked with {@link Stable} to trust the contents
* of the array. No other facility in JDK provides this functionality (yet).
* {@link Stable} is safe here, because value is never null.
*/
@Stable
private final byte[] value;

/**
* The identifier of the encoding used to encode the bytes in
* {@code value}. The supported values in this implementation are
*
* LATIN1
* UTF16
*
* @implNote This field is trusted by the VM, and is a subject to
* constant folding if String instance is constant. Overwriting this
* field after construction will cause problems.
*/
private final byte coder;

/** Cache the hash code for the string */
private int hash; // Default to 0

/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -6849794470754667710L;
static final boolean COMPACT_STRINGS;

static {
   COMPACT_STRINGS = true;
}

其他屬性:

@Native static final byte LATIN1 = 0;
@Native static final byte UTF16  = 1;
public static final Comparator<String> CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator();

賞析

相對於JDK1.8,String的成員變數做出了一些比較大的改動,將value由原先的char[]型別轉化為byte[]型別。意味著原先放在一個char中的字元需要放入1個byte或這2個byte中,這意味著我們需要考慮編碼的影響,String就提供了LATIN1和UTF16兩種編碼。

新增了幾個成員變數都是用來輔助儲存這個String物件採用了哪些編碼。