Community
Participate
Working Groups
Build I20031111 Explicit String allocations were recently removed from AST.toString and elsewhere. They were there for good reason (as the comment there suggested). The problem is that new String(stringBuffer) is not quite equivalent to StringBuffer.toString(). See the note to implementors on StringBuffer.toString(). The upshot is that a snippet like StringBuffer sb = new StringBuffer(6000); sb.append("foo"); return sb.toString(); will typically return a String object that occupies the space of a 6000 character string rather than a 3 character one. return new String(sb); allocate a new string of exactly the right size.
Incorrectly entered. Moving to JDT Core.
StringBuffer.toString() calls new String(sb). So I really don't see any memory improvement. We could get a small improvement in time.
You're quite quite. The Sun implementation of both "sb.toString()" and "new String(sb)" are equivalent and return a String object that occupies space proportional to sb.capacity(). On the other hand, "sb.substring(0)" returns a String object with space proportional to sb.length().
So I guess you'd like the latter to be used.
I will get rid of new StringBuffer(6000); and replace it with new StringBuffer(). Then we use the default behavior of the StringBuffer. Fixed and released in HEAD.
Verified for 3.0M6