Does StringBuilder.toString retain the built string?

走远了吗. 提交于 2019-12-23 12:21:10

问题


I'm creating a StringBuilder to collect strings that I periodically flush to a server. If the flush fails, I want to keep the strings to try again next time, although in the mean time I might get additional strings to send which must be added to the StringBuilder.

What I want to know is what the most efficient way to do this would be, as this is being done in an Android app where battery usage and thus CPU usage is a big concern. Does calling StringBuilder's toString() function store the resulting string it returns internally so that a subsequent call doesn't have to do the work of copying all the original strings over? Or if the call fails, should I create a new StringBuilder initialized with the return value from toString()?


回答1:


Here is the OpenJDK source code for StringBuilder:

public String toString() {
    // Create a copy, don't share the array
    return new String(value, 0, count);
}

The source for the String constructor with those parameters is:

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

So yes, it does create a new String everytime, and yes, it makes a copy of the char[] everytime.

It's important to note that this is one implementation of toString, and another implementation may obviously be different.




回答2:


It would be an implementation detail. Since java strings are immutable a correct impl can choose to share or create new strings from StringBuilder.toString() even if it's not needed.

As everyone says, you can test to see if this is indeed a real performance issue for you. If it is one (clunky) workaround is to wrap StringBuilder and cache the resulting string. You can use a dirty flag to indicate the content was modified.




回答3:


StringBuilder.toString API says that a new String object is allocated and initialized to contain the character sequence currently represented by this object.



来源:https://stackoverflow.com/questions/15724895/does-stringbuilder-tostring-retain-the-built-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!