I am trying to understand how the compiler views the following print statements. It is simple yet a bit intriguing.
This prints the added value. Convincing enough.<
The +
operator is overloaded in the compiler. If both operands are numeric, +
is addition. If either or both operands are String, +
is string concatenation. (If one operand is String and the other is numeric, the number is cast to a String). Finally, +
binds left-to-right.
All this causes a compound formula a + b + c + ...
to do addition left-to-right until it hits the first string operand, at which point it switches to string concatenation for the remainder of the formula. So...
"1" + 2 + 3 + 4 = 1234 /* cat cat cat */
1 + "2" + 3 + 4 = 1234 /* cat cat cat */
1 + 2 + "3" + 4 = 334 /* add cat cat */
1 + 2 + 3 + "4" = 64 /* add add cat */