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

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -