Ideally we should use new StringBuilder()
Digging a bit in StringBuilder class from grepcode I get to know the following.
Creating new object :
/**
* Creates an AbstractStringBuilder of the specified capacity.
*/
AbstractStringBuilder(int capacity) {
value = new char[capacity];
}
new StringBuilder() creates a new object with initial capacity char array.
Overhead here : GC will be called for clearing older object.
Using delete :
public AbstractStringBuilder delete(int start, int end) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (end > count)
end = count;
if (start > end)
throw new StringIndexOutOfBoundsException();
int len = end - start;
if (len > 0) {
System.arraycopy(value, start+len, value, start, count-end);
count -= len;
}
return this;
}
Using Length and TrimToSize :
public void trimToSize() {
if (count < value.length) {
value = Arrays.copyOf(value, count);
}
}
Will call copyOf From array class
public static char[] copyOf(char[] original, int newLength) {
char[] copy = new char[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
Now it will also call System.arrayCopy which is a native method.
Now if you see in copyOf we are creating a new charArray again of 0 length,
and when we try to add again data to it, it will call expand because the current length would be 0.
So I think its better to call new StringBuilder()
You can see the above code on grepcode
PS : @user3241961 is write in case you are using reference of this object then new would require to set it again