Reusing a StringBuffer is not as useful as you might think. Any time you call toString() and then make a change to the StringBuffer contents after, the internal char[] array will be replaced for you (whether you want to or not). This is because the StringBuffer has optimized the toString() operation by reusing the StringBuffer's internal char[] array as the internal char[] array for the new
String as well. This makes the new String creation very fast. But since the String must be immutable, StringBuffer can't make any further changes to that array. So the moment you try to make further changes, it will create a new internal array with the contents of the old. (That's what the variable "shared" is for - monitoring whether the char array is being used by an outside String or not.) This operation would also be the main cost of creating a whole new StringBuffer, if you were to do that. So in most cases it's cleaner to just create a new StringBuffer rather than reuse an old one.
BTW, when I look at the
Java source I don't see the same stuff you guys do. Are you looking at JDK 1.4.1, or an older version?