String In Java

JDK1.4中关于String的几个实现方法:

public String(String original) {
    this.count = original.count;
    if (original.value.length > this.count) {
        // The array representing the String is bigger than the new
        // String itself.  Perhaps this constructor is being called
        // in order to trim the baggage, so make a copy of the array.
        this.value = new char[this.count];
        System.arraycopy(original.value, original.offset,
        this.value, 0, this.count);
    } else {
        // The array representing the String is the same
        // size as the String, so no point in making a copy.
        this.value = original.value;
    }
}

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }

    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;

        if (n == anotherString.count) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = offset;
            int j = anotherString.offset;
            while (n-- != 0) {
                if (v1[i++] != v2[j++])
                return false;
            }
            return true;
        }
    }
    return false;
}