對String原始碼的理解

2021-08-17 08:04:19 字數 1697 閱讀 1576

參考的連線:

– 這個方法呼叫了arrays.copyofrange(value,from,to)這個方法,這個方法最後還是呼叫了system.arraycopy這個方法,來複製陣列

public

string(char

value, int offset, int count)

if (count <= 0)

if (offset <= value.length)

}// note: offset or count might be near -1>>>1.

if (offset > value.length - count)

this.value = arrays.copyofrange(value, offset, offset+count);

}

public boolean equals(object anobject) 

//判斷anobject是否是string類以及其子類的例項

if (anobject instanceof string)

return

true;}}

return

false;

}

public

intlength()

public

char charat(int

index)

return value[index];

}

//    如果首尾沒有空格則返回原來的陣列,有則呼叫substring方法

return ((st > 0) || (len < value.length)) ? substring(st, len) : this;

– 這個方法有兩種形式的,乙個是只有乙個int型別引數,乙個有兩個int型別引數

– 最後會判斷是否呼叫構造方法,而構造方法最後呼叫arrays.copyofrange這個方法,最後還是呼叫system.arraycopy這個方法

//乙個引數

int sublen = value.length - beginindex;

return (beginindex == 0) ? this : new string(value, beginindex, sublen);

//兩個引數

int sublen = endindex - beginindex;

return ((beginindex == 0) && (endindex == value.length)) ? this : new string(value, beginindex, sublen);

public

string split(string regex)

string類中的方法中只要是返回的string型別,則返回的結果都是新的string物件,除開一些特殊的例子

final修飾的類是不可以被繼承或修改的,也就是string類是不可以被繼承的;

底層採用final修飾的字串陣列

string類封裝了一系列的對字串進行操作的方法

原始碼級理解Java的String型別(之二)

string的hashcode 字串的拼接。流與字符集的轉換 codepointat 用法 我們知道integer的hashcode就是數值本身,那麼字串型別的如何計算的呢?原始碼中注釋已經說明了他的演算法 s 0 31 n 1 s 1 31 n 2 s n 1 同時 也已經給出了實現,這裡的 符號...

String原始碼總結

1.indexof 方法 if fromindex sourcecount if fromindex 0 if targetcount 0 char first target targetoffset int max sourceoffset sourcecount targetcount for ...

String原始碼解析

string類被final修飾詞修飾,代表不可修改的特性,它實現了三個介面,serializable是序列化介面,compareble是排序介面,char是字串行介面。public final class string implements serializable,comparable,chars...