Consider the following example:
class Quirky {
public static void main(String[] args) {
int x = 1;
int y = 3;
System.out.println(x =
==
is a binary equality operator.
The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.
Java 11 Specification > Evaluation Order > Evaluate Left-Hand Operand First
The thing here is the arithmatic operators/relational operators precedency order out of the two operators =
vs ==
the dominant one is ==
(Relational Operators dominates ) as it precedes =
assignment operators.
Despite precedence, the order of evaluation is LTR (LEFT TO RIGHT) precedence comes into picture after evaluation order.
So, Irrespective of any constraints evaluation is LTR.
It is not the same. The left hand side will always be evaluated before the right hand side, and the brackets don't specify an order of execution, but a grouping of commands.
With:
x == (x = y)
You are basically doing the same as:
x == y
And x will have the value of y after the comparison.
While with:
(x = y) == x
You are basically doing the same as:
x == x
After x took y's value. And it will always return true.
Expressions are evaluated from left to right. In this case:
int x = 1;
int y = 3;
x == (x = y)) // false
x == t
- left x = 1
- let t = (x = y) => x = 3
- x == (x = y)
x == t
1 == 3 //false
(x = y) == x); // true
t == x
- left (x = y) => x = 3
t = 3
- (x = y) == x
- t == x
- 3 == 3 //true
which, by the order implied by brackets, should be calculated first
No. It is a common misconception that parentheses have any (general) effect on calculation or evaluation order. They only coerce the parts of your expression into a particular tree, binding the right operands to the right operations for the job.
(And, if you don't use them, this information comes from the "precedence" and associativity of the operators, something that's a result of how the language's syntax tree is defined. In fact, this is still exactly how it works when you use parentheses, but we simplify and say that we're not relying on any precedence rules then.)
Once that's done (i.e. once your code has been parsed into a program) those operands still need to be evaluated, and there are separate rules about how that is done: said rules (as Andrew has shown us) state that the LHS of each operation is evaluated first in Java.
Note that this is not the case in all languages; for example, in C++, unless you're using a short-circuiting operator like &&
or ||
, the evaluation order of operands is generally unspecified and you shouldn't rely on it either way.
Teachers need to stop explaining operator precedence using misleading phrases like "this makes the addition happen first". Given an expression x * y + z
the proper explanation would be "operator precedence makes the addition happen between x * y
and z
, rather than between y
and z
", with no mention of any "order".
== is a comparison equality operator and it works from left to right.
x == (x = y);
here the old assigned value of x is compared with new assign value of x, (1==3)//false
(x = y) == x;
Whereas, here new assign value of x is compared with the new holding value of x assigned to it just before comparison, (3==3)//true
Now consider this
System.out.println((8 + (5 * 6)) * 9);
System.out.println(8 + (5 * 6) * 9);
System.out.println((8 + 5) * 6 * 9);
System.out.println((8 + (5) * 6) * 9);
System.out.println(8 + 5 * 6 * 9);
Output:
342
278
702
342
278
Thus, Parentheses plays its major role in arithmetic expressions only not in comparison expressions.