1. 程式人生 > >String的subString()方法實現內部機制

String的subString()方法實現內部機制

在網上看到一篇關於java的String的subString()方法內部實現機制。現在將其記錄下來。

JDK6:

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

    /** The offset is the first index of the storage that is used. */
    private final int offset;

    /** The count is the number of characters in the String. */
    private final int count;
    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.offset = 0;
        this.count = count;
        this.value = Arrays.copyOfRange(value, offset, offset+count);
    }
    public String substring(int beginIndex, int endIndex) {
    	if (beginIndex < 0) {
    	    throw new StringIndexOutOfBoundsException(beginIndex);
    	}
    	if (endIndex > count) {
    	    throw new StringIndexOutOfBoundsException(endIndex);
    	}
    	if (beginIndex > endIndex) {
    	    throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
    	}
    	return ((beginIndex == 0) && (endIndex == count)) ? this :
    	    new String(offset + beginIndex, endIndex - beginIndex, value);
    }
}

呼叫substring()方法時建立的新的String與以前的string仍指向同一個char[]陣列,只有offset(位置)以及count(計數)不同,由於char[]陣列一直被引用,無法回收,所以可能導致記憶體洩漏。

JDK7:

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

    /** The offset is the first index of the storage that is used. */
    private final int offset;

    /** The count is the number of characters in the String. */
    private final int count;
    
    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);
    }
    public String substring(int beginIndex, int endIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        if (endIndex > value.length) {
            throw new StringIndexOutOfBoundsException(endIndex);
        }
        int subLen = endIndex - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return ((beginIndex == 0) && (endIndex == value.length)) ? this
                : new String(value, beginIndex, subLen);
    }
}

JDK7在subString()時建立了一個新的陣列,解決JDK6 subString()所帶來的問題。