JLS string concatenation clarification
Martin Buchholz
martinrb at google.com
Tue Jan 21 10:41:25 PST 2014
Can we agree that when
String empty = "";
then
s + empty
should be permitted to be the same object as s.
On Tue, Jan 21, 2014 at 9:59 AM, Louis Wasserman <lowasser at google.com>wrote:
> JLS 15.18.1 states:
>
>> The String object is newly created (§12.5<http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.5>)
>> unless the expression is a compile-time constant expression (§15.28<http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28>
>> ).
>
> JLS 12.5 states:
>
>> Execution of a string concatenation operator (§15.18.1<http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.1>)
>> that is not part of a constant expression (§15.28<http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.28>)
>> sometimes creates a new String object to represent the result.
>
>
> It's not clear to me, from reading this, whether string concatenation with
> + *always* creates a new String object or *sometimes *creates a new
> String object. 15.18.1 seems to imply always, but 12.5 says "sometimes."
>
> My understanding is that String.concat used to be used for string
> concatenation, and String.concat currently returns its receiver if its
> argument is the empty string. This seems to be evidence for the
> "sometimes" interpretation.
>
> --
> Louis Wasserman
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20140121/5084c269/attachment.html
More information about the compiler-dev
mailing list