+ 3

Can sameone explain why the answear is "3Solo12" instead of "3Solo3"?

The code System.out.print(1+2+"Solo"+1+2);

5th Nov 2017, 9:15 AM
Quanz
5 Answers
+ 9
Addition is done from left to right, so firstly 1+2 is computed and return 3 because both values are numbers, then 3+"Solo" return "3solo" because 3 is turned implicitly to string to perform number+string concanetation, and then "3Solo"+1 in same way equals "3Solo1", and finaly last "3Solo1"+2 will return still same ways "3Solo12"... if you want to get "3Solo3" you should write 1+2+"Solo"+(1+2) to modify priority of operations ^^
5th Nov 2017, 9:23 AM
visph
visph - avatar
+ 10
Everything after "Solo" becomes a string because "Solo" is a string and when it concatenates with something else it converts to a string.
5th Nov 2017, 9:21 AM
qwerty
qwerty - avatar
+ 4
becouse after adding string, it understands not int sum but string sum. if you wirte add +(1+2) then it adds as int sum
5th Nov 2017, 9:18 AM
Mantelis
Mantelis - avatar
+ 3
Thank you :D
5th Nov 2017, 9:47 AM
Quanz