8 Answers

New AnswerThat is because in the first a+b, 10 and 20 are added to get 30, but when you add " ", it becomes a string("30 "). And when you add 10, 20 they are added like strings. You could put brackets on the second a+b. System.out.println(a+b+" "+(a+b)) Output: 30 30

The parentheses will ensure that 30 is computed both times, as opposed to adding strings together the second time.

You could change line 7 to : System.out.print(a+b+" "); And add another line of code: System.out.print(a+b); The reason i think is because after the space ,which is a string, the numbers get converted to the same data type to perform the concitation

Before the string concatenation, compiler treats a and b as integers thus giving right computation! But from the concatenation and the rests are treated as strings thus giving wrong results! You should put computation inside parenthesis to prevent this kind of unexpected results!

That is because in the first a+b, 10 and 20 are added to get 30, but when you add " ", it becomes a string("30 "). And when you add 10, 20 they are added like strings. You could put brackets on the second a+b. System.out.println(a+b+" "+(a+b)) Output: 30 30

That is because in the first a+b, 10 and 20 are added to get 30, but when you add " ", it becomes a string("30 "). And when you add 10, 20 they are added like strings. You could put brackets on the second a+b. System.out.println(a+b+" "+(a+b)) Output: 30 30