Why doesn't the Java compiler concatenate string literals if they appear after a variable?

Because the string concatenation operator (+) is syntactically left associative:

For example, the expression:

a + b + c

is always regarded as meaning:

(a + b) + c

Admittedly, if b and c are strings, the expression is equivalent to a + (b + c). So a compiler could do what you suggest in that specific situation, but it is not mandated by the specification...


String suffixConcat = i + "c" + "d";

is equivalent to

String suffixConcat = (i + "c") + "d";

You could argue that it is equivalent to this expression

String suffixConcat = i + ("c" + "d");

which will be optimized to

String suffixConcat = i + "cd";

I think, this it the reason why the byte code does not include that optimization is in Language specification (15.18.1. String Concatenation Operator +):

The String object is newly created (§12.5) unless the expression is a constant expression (§15.28).

In other words, (i + "c") has to be new string, and (i + "c") + "d" again has to be a new string.

However, a JIT compiler could internally apply the optimization as it does not change the observable semantics.


This isn't a great answer.

The order of addition is defined and takes place from the left.

This:

public static void main (String[] args) 
{
    String x= "X" + 1 + 2;
    String y= 1 + 2 + "Y";

    System.out.println(x);
    System.out.println(y);
}

Has expected output

X12
3Y

So I'm guessing that the compiler spots that literal plus literal can be optimized to 'long literal' but doesn't recognise that reordering the operations would have the same outcome so the verbose code produced.

Tags:

Java