public final class Stringimplements java.io.Serializable, Comparable<String>, CharSequence {/**char數組用于字符的存儲 */private final char value[];/** 緩存string的hash碼 */private int hash; // Default to 0public String() {/**無參構造函數,打印值為""*/this.value = new char[0];}public String(String original) {/**字符值和hash值均為參數的值*/ this.value = original.value;this.hash = original.hash;}public String(char value[]) {/**Arrays.copyOf()方法返回一個新構造的 char數組*/this.value = Arrays.copyOf(value, value.length);}public String(char value[], int offset, int count) {if (offset < 0) {throw new StringIndexOutOfBoundsException(offset);}if (count < 0) {throw new StringIndexOutOfBoundsException(count);}// Note: offset or count might be near -1>>>1.if (offset > value.length - count) {throw new StringIndexOutOfBoundsException(offset + count);}this.value = Arrays.copyOfRange(value, offset, offset+count);}/**傳入代碼點來構造String對象,代碼點和代碼單元相關內容可自行查閱資料,或者看我另一篇隨筆深入學習Java中的字符串,代碼點和代碼單元*/
public String(int[] codePoints, int offset, int count) {if (offset < 0) {throw new StringIndexOutOfBoundsException(offset);}if (count < 0) {throw new StringIndexOutOfBoundsException(count);}// Note: offset or count might be near -1>>>1.if (offset > codePoints.length - count) {throw new StringIndexOutOfBoundsException(offset + count);}final int end = offset + count;/**計算char數組的真實大小,一個代碼點有可能需要用2個char即2個代碼單元表示*/int n = count;for (int i = offset; i < end; i++) {int c = codePoints[i];if (Character.isBmpCodePoint(c))/**此代碼點為基本字符代碼點*/continue;else if (Character.isValidCodePoint(c))/**此代碼點為有效代碼點,即輔助字符代碼點,此時一個代碼點需要用2個char即2個代碼單元表示,n++*/n++;else throw new IllegalArgumentException(Integer.toString(c));}/**分配并填充字符數組*/final char[] v = new char[n];for (int i = offset, j = 0; i < end; i++, j++) {int c = codePoints[i];if (Character.isBmpCodePoint(c))v[j] = (char)c;elseCharacter.toSurrogates(c, v, j++);}this.value = v;}/**返回指定下標的字符*/public char charAt(int index) {if ((index < 0) || (index >= value.length)) {throw new StringIndexOutOfBoundsException(index);}return value[index];}/**返回指定下標的代碼點*/ public int codePointAt(int index) {if ((index < 0) || (index >= value.length)) {throw new StringIndexOutOfBoundsException(index);}return Character.codePointAtImpl(value, index, value.length);}public boolean equals(Object anObject) {if (this == anObject) {return true;/**如果是同一個對象,直接返回true*/}if (anObject instanceof String) {String anotherString = (String) anObject;int n = value.length;if (n == anotherString.value.length) {char v1[] = value;char v2[] = anotherString.value;int i = 0;while (n-- != 0) {if (v1[i] != v2[i])return false;i++;}return true;}}return false;}public boolean contentEquals(StringBuffer sb) {synchronized (sb) {/**鎖定StringBuffer對象,防止修改*/return contentEquals((CharSequence) sb);}}public boolean equalsIgnoreCase(String anotherString) {return (this == anotherString) ? true: (anotherString != null)&& (anotherString.value.length == value.length)&& regionMatches(true, 0, anotherString, 0, value.length);}/**計算并返回hsah碼*/ public int hashCode() {int h = hash;/**默認是0*/if (h == 0 && value.length > 0) {char val[] = value;for (int i = 0; i < value.length; i++) {h = 31 * h + val[i];}hash = h;}return h;}/**字串為新對象*/ public String substring(int beginIndex) {if (beginIndex < 0) {throw new StringIndexOutOfBoundsException(beginIndex);}int subLen = value.length - beginIndex;if (subLen < 0) {throw new StringIndexOutOfBoundsException(subLen);}return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);} /**連接字符串*/ public String concat(String str) {int otherLen = str.length();if (otherLen == 0) {return this;}int len = value.length;char buf[] = Arrays.copyOf(value, len + otherLen);str.getChars(buf, len);/**將str內的字符依次存放到buf數組中,下標從len開始*/return new String(buf, true);/**返回新生成的對象*/}/**trim()方法將字符串首尾的ascii碼數值小于或等于空格的字符刪除*/ public String trim() {int len = value.length;int st = 0;char[] val = value; /* avoid getfield opcode */while ((st < len) && (val[st] <= ' ')) {/**從字符串首個字符開始判斷ascii是否小于或等于空格,若小于或等于,計數++*/st++;}while ((st < len) && (val[len - 1] <= ' ')) {/**從字符串最后一個字符開始判斷ascii是否小于或等于空格,若小于或等于,計數--*len--;}return ((st > 0) || (len < value.length)) ? substring(st, len) : this;}/**返回自身對象的引用*/ public String toString() {return this;}/**和toString()唯一區別就是先判斷對象是否為空*/ public static String valueOf(Object obj) {return (obj == null) ? "null" : obj.toString();}
?