Here is the OpenJDK source code for StringBuilder :
public String toString() {
The source for the String constructor with these parameters:
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 creates a new String every time, and yes, it makes a copy of char[] every time.
It is important to note that this is one implementation of toString , and the other implementation may be different.
source share