欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

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

来源: javaer 分享于  点击 36495 次 点评:258

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


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

Java代码 收藏代码

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

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

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

StringBuilder.subString:

Stringbuilder.substring代码 收藏代码

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

String.subString:
Java代码 收藏代码

       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的构造函数是完全不同的,我们就来看看这两个代码:

Java代码 收藏代码

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对象否则你会死得很难看。
http://edwardpro.iteye.com/blog/920090

相关文章

    暂无相关文章

用户点评