There are two problems here: the octal literal and the evaluation order.
int y = 013 equivalent to int y = 11 , because 13 in base 8 is 11 in base 10.
For the evaluation order, the operator + is evaluated from left to right, so "x+y = " + x+y equivalent to ("x+y = " + x)+y , not "x+y = " + (x+y) . Spaces in Java are not significant.
Look at the following diagram ( sc is a concatenation of strings, aa is an arithmetic addition):
("x+y = " + x)+y | | (1) sc | | sc (2) "x+y = " + (x+y) | | | aa (1) | sc (2)
In both diagrams (1) occurs before (2) .
Without parentheses, the compiler evaluates from left to right (according to priority rules).
"x+y = " + x+y | | (1) | | (2)
source share