String concatenation order of operations
Liam Miller-Cushon
cushon at google.com
Wed Sep 8 00:40:45 UTC 2021
I noticed that the result of string concatenation is different for the
invokedynamic and inline strategies when the arguments have side effects.
With the inline strategy, each subexpression is evaluated and converted to
a string in order. With indy each subexpression is evaluated in order *but
not converted to a string*, and later each subexpression is converted to a
string in order.
Is one of these behaviours more correct than the other?
class T {
static String test() {
StringBuilder builder = new StringBuilder("foo");
return "" + builder + builder.append("bar");
}
public static void main(String[] args) {
System.err.println(test());
}
}
$ javac -XDstringConcat=inline T.java ; java T
foofoobar
$ javac -XDstringConcat=indy T.java ; java T
foobarfoobar
I read some of the related discussion about the spec requirements for
string concatenation in [1], and also some of the discussion around
JEP-280, and didn't find a conclusive answer. There was a bug [2] about the
evaluation order of string concat subexpressions with indy, but that was
about converting the arguments to strings in the wrong order.
[1]
http://mail.openjdk.java.net/pipermail/compiler-dev/2015-March/009357.html
[2] https://bugs.openjdk.java.net/browse/JDK-8200118
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20210907/3cff8ce2/attachment.htm>
More information about the compiler-dev
mailing list