String concatenation versus StringBuffer

D

Darren

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!
 
E

Eric Sosman

Darren said:
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.
 
G

Grant Wagner

Darren said:
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 />
 
R

Roedy Green

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.
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top