+ 3

Java question

Why System.out.print("2+2="+2+2) Is 2+2=22 instead of 2+2=4?

20th Sep 2019, 5:24 AM
R.J
R.J - avatar
2 Answers
+ 2
You are adding two things to a string. The execution order is from left to right. When you add a number to a string, the number is implicitly converted to a string in the background, and the two strings are concatenated. If you want to print the result of the numeric addition, you should use parentheses to force java to add the numbers first, then concatenate the result to the text. System.out.print("2+2=" + (2+2))
20th Sep 2019, 9:01 AM
Tibor Santa
Tibor Santa - avatar