Why does this code work correctly in Java? I have tried it in Eclipse and Itellij IDEA. The result is 8.
int a = 3, b = 5;
System.out.println(a + + b);
Why does this code work correctly in Java? I have tried it in Eclipse and Itellij IDEA. The result is 8.
int a = 3, b = 5;
System.out.println(a + + b);
In java, +
plays many roles according to the type of variables it is acting on. For numeric variables +
is a binary and also a unary operator.
So in your statement
System.out.println(a + + b);
the compiler consider +b
as an expression with the unary operator +
(which does nothing), and then add a
to that expression using the other +
that is consider now as a binary operator. To better understand this, you should read about the priority of operators.