1. 程式人生 > >String源碼j簡單分析

String源碼j簡單分析

other -1 bytes pub point bool unicode編碼 方法 matches

分析:

1、

 private final char value[];

String內部由這個char數組維護String的字符。首先String類用final修飾,不可繼承,其次,value[]用 fianl修飾,代表引用不可變。

    public String() {
        this.value = new char[0];
    }

當調用無參構造方法時,將char數組初始化為char[0]。

2、 String中的codePoint

codePoint 舉例來說: “我”->對應的codePoint 為十進制的25105->十六進制的6211->UNICODE編碼表中的6211(“我”字在UNICODE編碼表中對應的16進制數)

3、

public byte[] getBytes(String charsetName)
            throws UnsupportedEncodingException {
        if (charsetName == null) throw new NullPointerException();
        return StringCoding.encode(charsetName, value, 0, value.length);
    }

根據某編碼格式編碼

4、equals方法

 public boolean equals(Object anObject) {
        
if (this == anObject) { return 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; }

5、測試兩個字符串區域是否相等。

    public boolean regionMatches(int toffset, String other, int ooffset,
            int len) {
        char ta[] = value;
        int to = toffset;
        char pa[] = other.value;
        int po = ooffset;
        // Note: toffset, ooffset, or len might be near -1>>>1.
        if ((ooffset < 0) || (toffset < 0)
                || (toffset > (long)value.length - len)
                || (ooffset > (long)other.value.length - len)) {
            return false;
        }
        while (len-- > 0) {
            if (ta[to++] != pa[po++]) {
                return false;
            }
        }
        return true;
    }

6、hashcode

返回此字符串的哈希碼。 String 對象的哈希碼根據以下公式計算:

 s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
 

使用 int 算法,這裏 s[i] 是字符串的第 i 個字符, n 是字符串的長度, ^ 表示求冪。(空字符串的哈希值為 0。)

    public int hashCode() {
        int h = hash;
        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;
    }

7、截取子串,返回的是new 的String

    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);
    }

8、字符串拼接,返回的時new String,所以不建議多次拼接,多次拼接請選StringBuffer

    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);
        return new String(buf, true);
    }

9、valueOf方法要註意,如果傳進來的字符串為null,則會自動new String("null")返回,否則返回對象.toString()

    public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }

String源碼j簡單分析