JLS string concatenation clarification

Louis Wasserman lowasser at google.com
Tue Jan 21 09:59:36 PST 2014


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/24476c0f/attachment.html 


More information about the compiler-dev mailing list