- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I sure hope not. It's a really stupid automatic tool if it was.
Kelly
Admin
<font style="BACKGROUND-COLOR: #efefef">Does't Java automatically use a StringBuffer when you spell things the easy way? (E.g., a.toString() + ", " + b.toString().)
The C# compiler uses <font size="3">
</font>in that case. So, when you're building a one-off string, not in a loop, have a reasonable number of terms, don't get so tricky, just make it readable. :)</font>
Admin
I'm not positive that Java doesn't do this, but I'm pretty sure it doesn't. This would be a very specific optimization, and from what I've seen in the past at least, it doesn't seem that the sun java compiler automatically does those. I could be wrong, of course... I just seem to remember decompiling several things in my research and finding them very poorly (actually not at all) optimized.
That being said, this would be an easy "no-brainer" optimization and so they may be doing it in their compiler. However, I'm very skeptical that any such thing would be the "standard" way of doing things - i.e. that all compilers would agree in their implementation of this type of construct.
Also, since there is overhead in creating the stringbuffer object, there may be cases (i.e. 1 concat, 2 concats, 3 concats) where depending on the length of the strings it may be better to "do what the user said" rather than optimizing using StringBuffer.
Kelly
Admin
Sure, make me look it up. :) It's an allowed optimization per the 2.0 Java language spec, which doesn't speak to whether any compiler does it. I wonder where my little morsel of questionable memory came from... the usual place, no doubt.
____
<font color="#000080"><font size="1">15.18.1.2 </font><font size="1">Optimization of String Concatenation
An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.</font></font>
<font color="#000080" size="1">For primitive types, an implementation may also optimize away the creation of a wrapper object by converting directly from a primitive type to a string.
</font>Admin
Sorry, just couldn't bring myself to be interested enough in this to go look for myself.
Kelly
Admin
Looks nice, though.