java9新特性-11-String存儲結構變更
1. 官方Feature
JEP254: Compact Strings
2. 產生背景
Motivation
The current implementation of the String classstores characters in a char array,using two bytes (sixteen bits) for each character. Data gathered from manydifferent applications indicates that strings are a major component of heapusage and, moreover, that most String objectscontain only Latin-1 characters. Such characters require only one byte ofstorage, hence half of the space in the internal char arraysof such String objects is going unused.
3. 使用說明
Description
We propose to change the internal representation of the Stringclass from a UTF-16 char array to a byte array plus an encoding-flag field. The new String class will store characters encoded either as ISO-8859-1/Latin-1 (one byte per character), or as UTF-16 (two bytes per character), based upon the contents of the string. The encoding flag will indicate which encoding is used.
?結論:String 再也不用 char[] 來存儲啦,改成了 byte[] 加上編碼標記,節約了一些空間。
4. 拓展:StringBuffer 與StringBuilder
那StringBuffer 和 StringBuilder 是否仍無動於衷呢?
?String-related classes such as AbstractStringBuilder, StringBuilder, and StringBuffer will
be updated to use the same representation, as will the HotSpot VM‘s intrinsic string operations.
作者:尚矽谷面試官宋紅康
鏈接:https://www.jianshu.com/p/39ae521056c2
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請註明出處。
java9新特性-11-String存儲結構變更