原來String是這樣的(下)

前言

回顧

我們講到了String的equals和==的區(qū)別:

equals根據(jù)你編寫的方法體來進(jìn)行比較,而==是根據(jù)比較的引用地址是否相同來比較的。

主題

今天我們來講講String類的其他.方法在源碼中的解讀.

例子

publicclassTest{

publicstaticvoidmain(String[] args){

String a1=newString("abc");

String a2=newString("abc");

System.out.println(a1.isEmpty());

System.out.println(a1.length());

System.out.println(a1.charAt(1);

System.out.println(a1.substring(2,3));

}

}

String的幾個(gè)方法

這里我們介紹了String的幾個(gè)簡單的方法。

.isEmpty();

.length();

.charAt();

.subString();

我們查看源碼就可以知道:String方法的幾個(gè)構(gòu)造器

.isEmpty();

/**

* Returns {@codetrue} if, and only if, {@link#length()} is {@code0}.

*

*@return{@codetrue} if {@link#length()} is {@code0}, otherwise

* {@codefalse}

*

*@since1.6

*/

publicbooleanisEmpty(){

returnvalue.length ==0;

}

isEmpty直接判斷傳進(jìn)來的值長度是否為0

.length();

/**

* Returns the length of this string.

* The length is equal to the number of Unicode

* code units in the string.

*

*@returnthe length of the sequence of characters represented by this

*? ? ? ? ? object.

*/

publicintlength(){

returnvalue.length;

}

返回字符串的長度

.charAt();

/**

* Returns the {@codechar} value at the

* specified index. An index ranges from {@code0} to

* {@codelength() - 1}. The first {@codechar} value of the sequence

* is at index {@code0}, the next at index {@code1},

* and so on, as for array indexing.

*

*

If the {@codechar} value specified by the index is a

* surrogate, the surrogate

* value is returned.

*

*@paramindex? the index of the {@codechar} value.

*@returnthe {@codechar} value at the specified index of this string.

*? ? ? ? ? ? The first {@codechar} value is at index {@code0}.

*@exceptionIndexOutOfBoundsException? if the {@codeindex}

*? ? ? ? ? ? argument is negative or not less than the length of this

*? ? ? ? ? ? string.

*/

publiccharcharAt(intindex){

if((index <0) || (index >= value.length)) {

thrownewStringIndexOutOfBoundsException(index);

}

returnvalue[index];

}

charAt這里我們可以看到,類型為char,定義了一個(gè)下標(biāo)

如果下標(biāo)index<0或者>=字符的長度拋出StringIndexOutOfBoundsException

否則返回value[index] 這里我們返回的就是vlaue[1] 答案為b

.subString();

**

* Returns a string that is a substring of this string. The

* substring begins at the specified {@code beginIndex} and

* extends to the character at index {@code endIndex - 1}.

* Thus the length of the substring is {@code endIndex-beginIndex}.

*

* Examples:

*

* "hamburger".substring(4, 8) returns "urge"

* "smiles".substring(1, 5) returns "mile"

*

*

* @param? ? ? beginIndex? the beginning index, inclusive.

* @param? ? ? endIndex? ? the ending index, exclusive.

* @return? ? the specified substring.

* @exception? IndexOutOfBoundsException? if the

*? ? ? ? ? ? {@code beginIndex} is negative, or

*? ? ? ? ? ? {@code endIndex} is larger than the length of

*? ? ? ? ? ? this {@code String} object, or

*? ? ? ? ? ? {@code beginIndex} is larger than

*? ? ? ? ? ? {@code endIndex}.

*/

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

}

subString方法有兩個(gè)參數(shù),一個(gè)起始指標(biāo),一個(gè)結(jié)束指標(biāo)

判斷beginIndex < 0拋出StringIndexOutOfBoundsException,同樣的結(jié)束index大于長度拋出StringIndexOutOfBoundsException

然后定義一個(gè)sbuLen,為endIndex - beginIndex,判斷subLen小于0拋出StringIndexOutOfBoundsException

返回如果(beginIndex == 0) && (endIndex == value.length)返回對(duì)象本身,否則該數(shù)組和beginIndex、subLen`構(gòu)成新的對(duì)象返回

這里講的就是String基本的幾個(gè)方法。這里還有一個(gè)小彩蛋就是&和&&的區(qū)別

大家可以回憶一下.

&:方法無論前值是否為true,都要判斷后面的邏輯表達(dá)式

&&:會(huì)形成短路,前面為false的時(shí)候就不執(zhí)行后面邏輯

結(jié)尾附上String的基本方法:

/**

* Initializes a newly created {@codeString} object so that it represents

* an empty character sequence.? Note that use of this constructor is

* unnecessary since Strings are immutable.

*/

publicString(){

this.value ="".value;

}

/**

* Initializes a newly created {@codeString} object so that it represents

* the same sequence of characters as the argument; in other words, the

* newly created string is a copy of the argument string. Unless an

* explicit copy of {@codeoriginal} is needed, use of this constructor is

* unnecessary since Strings are immutable.

*

*@paramoriginal

*? ? ? ? A {@codeString}

*/

publicString(String original){

this.value = original.value;

this.hash = original.hash;

}

這就是String構(gòu)成其實(shí)是一個(gè)數(shù)組,有value和hash兩個(gè)屬性,大家可以多多理解和想象一下.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容