John Ersatznom wrote:
>> Say, does anyone know if code like:
>>
[quoted text clipped - 4 lines]
>> method call as if a single string literal had been used, without any
>> messy temporary StringBuffers under the hood?
> Not only do some Java compilers do it, but all Java compilers are /required/ to
> do it -- the details are part of the Java language spec (I can't be bothered to
> look up the relevent section of the JLS offhand).
Not true. According to JLS 3.10.5,
"String literals-or, more generally, strings that are the values of constant
expressions (§15.28)-are "interned" so as to share unique instances, using the
method String.intern."
That only applies to literals.
It goes on to say,
"Strings computed by concatenation at run time are newly created and therefore
distinct."
The expression 'foo + "another literal" ' is computed by concatenation at run
time, so it is distinct.
- Lew
John W. Kennedy - 05 Jan 2007 03:14 GMT
> John Ersatznom wrote:
>>> Say, does anyone know if code like:
[quoted text clipped - 18 lines]
>
> That only applies to literals.
No, it applies to literals and constant expressions.
A final initialized String is a "constant variable" (4.12.4).
An expression containing only constant variables and literals is a
constant expression.

Signature
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
Lew - 05 Jan 2007 04:53 GMT
> No, it applies to literals and constant expressions.
>
> A final initialized String is a "constant variable" (4.12.4).
>
> An expression containing only constant variables and literals is a
> constant expression.
Man, I never get tired of learning this language. It is delightfully nuanced.
Thank you for the clarification.
-Lew