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.