String concatenation versus StringBuffer  
Author Message
Darren





PostPosted: 2004-7-27 17:40:00 Top

java-programmer, String concatenation versus StringBuffer Can someone deny or confirm that the latest sdks convert code like

"abc" + "def" + "ghi"

into the appropiate StringBuffer code

new StringBuffer( "abc" ).append( "def" ).append( "ghi" )

automatically? Or is this some dream I had?

Thanks!

 
Eric Sosman





PostPosted: 2004-7-28 0:52:00 Top

java-programmer >> String concatenation versus StringBuffer Darren wrote:
> Can someone deny or confirm that the latest sdks convert code like
>
> "abc" + "def" + "ghi"
>
> into the appropiate StringBuffer code
>
> new StringBuffer( "abc" ).append( "def" ).append( "ghi" )
>
> automatically? Or is this some dream I had?

It was a dream.

javac does the concatenation of string literals and
`final' strings at compile time rather than at run time,
so your example just becomes "abcdefghi" with no StringBuffer
involvement at all.

If the concatenated pieces aren't all constants, javac
generates StringBuffer operations that are just a little
bit different from your sample. For example,

"abc" + anyString + "ghi";

produces the equivalent of

new StringBuffer()
.append("abc").append(anyString).append("ghi")
.toString()

Oddly enough, compile-time concatenation of constant
strings doesn't always take place when non-constants are
also involved. For example,

"aaa" + "bbb" + anyString + "xxx" + "yyy"

becomes

"aaabbb" + anyString + "xxx" + "yyy"

where the first two pieces have been concatenated by the
compiler but the final two have not. The compiler is
presumably being scrupulous about the left-to-right
associativity of the `+' operator, refusing to treat
the original as

"aaa" + "bbb" + anyString + ("xxx" + "yyy")

which does indeed become

"aaabbb" + anyString + "xxxyyy"

You can answer many questions of this sort for yourself
by disassembling the bytecodes with the javap program.

--
email***@***.com

 
Grant Wagner





PostPosted: 2004-7-28 1:06:00 Top

java-programmer >> String concatenation versus StringBuffer Darren wrote:

> Can someone deny or confirm that the latest sdks convert code like
>
> "abc" + "def" + "ghi"
>
> into the appropiate StringBuffer code
>
> new StringBuffer( "abc" ).append( "def" ).append( "ghi" )
>
> automatically? Or is this some dream I had?
>
> Thanks!

Well, that depends, if you do: String s = "abc" + "def" + "ghi";

The compiler turns it into: String s = "abcdefghi";

However, if you do:

String s = "";
for (int i = 0; i < len; i++) { s += "xxx"; }

Then that is definitely compiled as:

s = (new StringBuffer()).append(s).append("xxx").toString();

although it may be optimized to:

s = (new StringBuffer(s)).append("xxx").toString();

By the way, this is the reason it's a Bad Thing(tm) to do that sort of
String concatentation in a loop. Something like the following is
probably better:

StringBuffer sb = new StringBuffer(); // or new StringBuffer(len *
3);?
for (int i = 0; i < len; i++) { sb.append("xxx"); }
String s = sb.toString();

This is covered in the Java 2 documentation: <url:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/StringBuffer.html />

 
 
Roedy Green





PostPosted: 2004-7-28 6:22:00 Top

java-programmer >> String concatenation versus StringBuffer On Tue, 27 Jul 2004 10:39:34 +0100, Darren <darren@localhost> wrote or
quoted :

>Can someone deny or confirm that the latest sdks convert code like
>
>"abc" + "def" + "ghi"
>
>into the appropiate StringBuffer code
>
>new StringBuffer( "abc" ).append( "def" ).append( "ghi" )
>
>automatically? Or is this some dream I had?
they do better than that. they convert it into one big string
literal.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
 
Roedy Green





PostPosted: 2004-7-28 6:26:00 Top

java-programmer >> String concatenation versus StringBuffer On Tue, 27 Jul 2004 17:06:23 GMT, Grant Wagner
<email***@***.com> wrote or quoted :

>StringBuffer sb = new StringBuffer(); // or new StringBuffer(len *
>3);?
>for (int i = 0; i < len; i++) { sb.append("xxx"); }
>String s = sb.toString();

Again most of the time you would never notice the difference, so it is
best to code for clarity. Sometimes these optimisations are done for
you by the compiler or runtime.

However, I make it a habit to use sb for loops or for extensive
x += work. since that it what other coders would expect.

For one liners, I use a great string of +, since the compiler will
generate optimal code for me anyway much more tersely.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
 
Darren





PostPosted: 2004-7-28 17:36:00 Top

java-programmer >> String concatenation versus StringBuffer Thanks all for the detailed responses - I feel I can become a String
master now! :)

Darren wrote:
> Can someone deny or confirm that the latest sdks convert code like
>
> "abc" + "def" + "ghi"
>
> into the appropiate StringBuffer code
>
> new StringBuffer( "abc" ).append( "def" ).append( "ghi" )
>
> automatically? Or is this some dream I had?
>
> Thanks!
>