substring(int beginIndex, int endIndex)
方法在jdk 6和jdk 7中的實現是不同的。了解他們的區別可以幫助你更好的使用他。為簡單起見,后文中用substring()
代表substring(int beginIndex, int endIndex)
方法。
substring() 的作用
substring(int beginIndex, int endIndex)
方法截取字符串並返回其[beginIndex,endIndex-1]范圍內的內容。
String x = "abcdef";
x = x.substring(1,3);
System.out.println(x);
輸出內容:
bc
調用substring()時發生了什么?
你可能知道,因為x是不可變的,當使用x.substring(1,3)
對x賦值的時候,它會指向一個全新的字符串:

然而,這個圖不是完全正確的表示堆中發生的事情。因為在jdk6 和 jdk7中調用substring時發生的事情並不一樣。
JDK 6中的substring
String是通過字符數組實現的。在jdk 6 中,String類包含三個成員變量:char value[]
, int offset
,int count
。他們分別用來存儲真正的字符數組,數組的第一個位置索引以及字符串中包含的字符個數。
當調用substring方法的時候,會創建一個新的string對象,但是這個string的值仍然指向堆中的同一個字符數組。這兩個對象中只有count和offset 的值是不同的。

下面是證明上說觀點的Java源碼中的關鍵代碼:
//JDK 6 String(int offset, int count, char value[]) { this.value = value; this.offset = offset; this.count = count; } public String substring(int beginIndex, int endIndex) { //check boundary return new String(offset + beginIndex, endIndex - beginIndex, value); }
JDK 6中的substring導致的問題
如果你有一個很長很長的字符串,但是當你使用substring進行切割的時候你只需要很短的一段。這可能導致性能問題,因為你需要的只是一小段字符序列,但是你卻引用了整個字符串(因為這個非常長的字符數組一直在被引用,所以無法被回收,就可能導致內存泄露)。在JDK 6中,一般用以下方式來解決該問題,原理其實就是生成一個新的字符串並引用他。
x = x.substring(x, y) + ""
JDK 7 中的substring
上面提到的問題,在jdk 7中得到解決。在jdk 7 中,substring方法會在堆內存中創建一個新的數組。

Java源碼中關於這部分的主要代碼如下:
//JDK 7 public String(char value[], int offset, int count) { //check boundary this.value = Arrays.copyOfRange(value, offset, offset + count); } public String substring(int beginIndex, int endIndex) { //check boundary int subLen = endIndex - beginIndex; return new String(value, beginIndex, subLen); }
來自:http://www.hollischuang.com/archives/1232