One extra note: it seems that all public ways of creating a new String
instance involves necessarily the copy of whatever buffer you are working with, be it a char[]
, a StringBuffer
or a StringBuilder
. From the String
javadoc (and is repeated in the respective toString
methods from the other classes):
The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.
So you'll end up having a possibly big memory copy operation after the "fast filling" of the array. The only solution that may avoid this issue is the one from @mlk, if you can manage working directly with the proposed CharSequence
implementation (what may be the case).
PS: I would post this as a comment but I don't have enough reputation to do that yet.