java - output for System.out.println gives strange number -
system.out.println("1 + 2 = " + 1 + 2);
output: 12
can explain why ? tried through documentation did not find anything...
because +
operator works left right, adds string "1 + 2 = "
1
first, , gets "1 + 2 = 1"
, adds 2
"1 + 2 = 12"
.
it's equivalent to
system.out.println(("1 + 2 = " + 1) + 2);
try instead.
system.out.println("1 + 2 = " + (1 + 2));
Comments
Post a Comment