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

Popular posts from this blog

css - I want to align grid in center -

python - SWIG function not printing output -

Contact Form PHP Email Script -