0

a + b + "see" + a + b

int a = 2 int b = 3 System.out.print(a+b+"see"+a+b); This comes up repeatedly in challenges, and although I know the answer by now, I wonder what makes the output of the above code 5see23, instead of either 5see5 or 23see23... Sorry if it comes up often, thanks for the answer.

12th Jun 2017, 11:25 PM
Stepan Lorenc
Stepan Lorenc - avatar
2 Respuestas
+ 3
I believe, the reason is the order in which the parameters are evaluated. I imagine that they are evaluated from the left to the right. So, a + b is evaluated first. Because they are to integers the value is 5. Then this result (int) is added to the string "see". Now, adding a string to an int makes the int be casted to string. The result is a concatenation "5see". Now, this is further concatenated with the other ints which are also casted to string.
13th Jun 2017, 12:35 AM
Ulisses Cruz
Ulisses Cruz - avatar
0
The program evaluates it first from mathematical order, and if it's all the same, then from left to right. if you want 5see5 - (a+b)+"see"+(a+b); or if you want 23see23 you need to use the ToString(); method.
13th Jun 2017, 5:42 AM
Shahar Levy
Shahar Levy - avatar