我们知道 String 是不可变对象,直至其 value 是不可变的,所以 String 对象的所有的修改方法,基本上都是 new String 然后返回。
今天就来讨论下 substring 方法,该方法也不列外,最后也是返回一个新建的对象,但是底层的实现在jdk 历史上却有着区别。
JDK 1.6
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);
}
再往下看一层:
// Package private constructor which shares value array for speed.
String(int offset, int count, char value[]) {
this.value = value;
this.offset = offset;
this.count = count;
}
不知道大家有没有发现问题,如果没有的话,我们继续看 jdk 1.8 的代码(事实上从1.6后就变了1.7也是一样)
JDK 1.6 +
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);
}
和 1.6 对比,好像没有什么变化,同样的还是往下一层看:
public String(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count <= 0) {
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
if (offset <= value.length) {
this.value = "".value;
return;
}
}
// 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);
}
相信到这里,大家应该看出了差别了吧。
1.6 偷懒,直接将调用对象的 value 数组直接传给了新对象的value 数组,存在了一条引用,导致原来的对象可能就没办法被回收,如果项目中大量使用的话,可能就会存在内存泄漏的风险!!
1.6后官方采用了数组复制的方式修复这一 bug !
如果觉得还不错的话,关注、分享、在看(关注不失联~), 原创不易,且看且珍惜~