On 2000-07-05 12:42:21 -0700, Man Chi Ly wrote:
> On Wed, 5 Jul 2000, Tony J. Paul wrote:
> > it like this,
> >
> > StringBuffer sb=new StringBuffer();
> > sb.append("SELECT COF_NAME, ");
> > sb.append("SALES FROM COFFEES ");
> > stmt.executeQuery(sb.toString());
>
> I recall reading somewhere that the following code is optimized by javac
> (roughly) into the form you provided just above:
>
> String s;
> s = "SELECT COF_NAME, ";
> s += "SALES FROM COFFEES ");
> s += "WHERE COF_NAME = 'Starbucks'";
> stmt.executeQuery(s);
>
> In other words, the compiler optimizes successive calls to the += operator
> into StringBuffer.append() calls without creating a bunch of intermediate
> objects. Does javac actually do this, or do I need to go back to the ugly
> StringBuffer idiom for efficiency?
Newer versions of javac do this. But they start with a
StringBuffer with default size (which is 16 characters). Since
the cost of expanding a StringBuffer is quite high and this will
happen often if you concatenate large strings, doing it yourself
with a initially large StringBuffer is faster.
Best regards
Martin
P.S.: Please read http://members.aol.com/intwg/guide.htm
--
Martin Schröder, [EMAIL PROTECTED]
ArtCom GmbH, Grazer Straße 8, D-28359 Bremen
Voice +49 421 20419-44 / Fax +49 421 20419-10
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]