Remember that in Java, an operator (like +
) can be overloaded. That means it will do different things, depending on its operands. For +
, there are (at least) two choices: integer addition and string concatenation.
Which overload is chosen depends more so on the left-hand-side operand. Also, string concatenation with a non-string operand can cause automatic conversion to a string.
The whole thing will be evaluated left-to-right like this:
x + y + " = " + y + x
3 + 5 + " = " + 3 + 5 // 3+5 chooses the integer addition overload of +
8 + " = " + 3 + 5 // 8 + " = " chooses string concatenation
"8" + " = " + 3 + 5 // so 8 is converted to "8" first
"8 = " + 3 + 5 // and then concatenated with " = "
"8 = " + "3" + 5 // now 3 is converted to "3"
"8 = 3" + 5 // and concatenated with "8 ="
"8 = 3" + "5" // finally 5 is converted to "5"
"8 = 35" // and concatenated with the rest
FWIW, it's ambiguity like this that leads me to dislike implicit conversions1.
1 - In C#. I love it in Python :-)