StringBuffer's SourceCode has a variable toStringCache
, but only in the toString()
method does it get a value:
public synchronized String toString() {
if (toStringCache == null) {
toStringCache = Arrays.copyOfRange(value, 0, count);
}
return new String(toStringCache, true);
}
Why not use toString
like StringBuilder
:
return new String(value, 0, count);
Is there any particular reason for this difference?