這裏是Java中的構造函數String對象之一:請給我解釋一下Java中String構造函數的代碼片段?
public String(String original) {
int size = original.count;
char[] originalValue = original.value;
char[] v;
if (originalValue.length > size) {
// 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.
int off = original.offset;
v = Arrays.copyOfRange(originalValue, off, off+size);
} else {
// The array representing the String is the same
// size as the String, so no point in making a copy.
v = originalValue;
}
this.offset = 0;
this.count = size;
this.value = v;
}
的代碼行,如果(originalValue.length>大小)是我關心的,我不認爲這種情況可能是對於正在執行的IF內的所有代碼,都爲true。字符串實際上是一個字符數組。 original.count應該等於它的值的長度(它的值是一個字符數組),所以條件不會發生。
我可能是錯的,所以我需要你的解釋。謝謝你的幫助。
VipHaLong。
http:// stackoverflow。com/questions/12907986/how-could-originalvalue-length-size-happen-in-the-string-constructor?rq = 1 – Mob