`
edwardpro
  • 浏览: 300542 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

StringBuilder和String中的subString方法的细微差别

 
阅读更多

昨天改了一个很小的地方,但是在线上跑的时候突然感觉内存消耗差别很多,而代码中仅仅增加了这么一句话:

int indexOfNocheck = linkBuffer.indexOf(NOCHECK_TAG);
ret.append(linkBuffer.substring(0, indexOfNocheck));

 ret也是一个Stringuilder,实际上我的直觉问题就应该出在了subString上。

 

于是翻出了StringBuilder和String中的subString的实现差别:

StringBuilder.subString:

    public String substring(int start, int end) {
	if (start < 0)
	    throw new StringIndexOutOfBoundsException(start);
	if (end > count)
	    throw new StringIndexOutOfBoundsException(end);
	if (start > end)
	    throw new StringIndexOutOfBoundsException(end - start);
        return new String(value, start, end - start);
    }

String.subString:

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

  看起来差别不大,他们都是采用了new String对象的方法,但是差别就在这里,这里使用的String的构造函数是完全不同的,我们就来看看这两个代码:

StringBuilder:
    public String(char value[], int offset, int count) {
        if (offset < 0) {
            throw new StringIndexOutOfBoundsException(offset);
        }
        if (count < 0) {
            throw new StringIndexOutOfBoundsException(count);
        }
        // Note: offset or count might be near -1>>>1.
        if (offset > value.length - count) {
            throw new StringIndexOutOfBoundsException(offset + count);
        }
        this.offset = 0;
        this.count = count;
        this.value = Arrays.copyOfRange(value, offset, offset+count);
    }
String:
    // 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;
    }

 原来差别在这里,StringBuilder是“外人”所以他使用的是copy的方法,而String内部的方法则调用了一个本地的构造函数,它返回的依然是它自己,只是修改了offset!

那么我的问题也得到了解答,实际上Stringbuilder中的subString在高并发系统里性能会差一些因为他会多分配对象,特别是当你反复使用subString方法的时候一定要记得使用String对象否则你会死得很难看。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics