On Sat, 2003-11-15 at 05:30, Christopher Schultz wrote:
> Steve,
> 
> > Maybe this is common knowledge, but you know never to do this right?
> > 
> > String x = "a" + "b";
> > 
> > You have to do something like this:
> > 
> > StringBuffer b = new StringBuffer();
> > b.append("a");
> > b.append("b");
> > 
> > Using StringBuffer vastly improves performance.
> 
> Ugh! This is like saying "never use goto". In this example, you are 
> completely wrong:

True, but there is one situation where the advice rings very true.

If you have to build a large string such as a report from some input
like a database result set, then using the + operator in a loop is
asking for poor performance. I recently found such code and replaced it
with a StringBuffer allocated before the loop and turned into a string
after the loop. This appears to have saved the creation of a
stringbuffer and a string for each iteration, as the performance was
radically improved.

In general, I use stringbuffers when my string concatination can't be
done on one line. If it can be done on one line you gain nothing from
explicitly using stringbuffers. I'm not sure if the compiler would merge
String concatination from concecutive lines, it certainly didn't in my
example above when a loop was involved.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to