Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# Evaluation Order.

Vicken Karaoghlanian
Ranch Hand
Posts: 522
from this code

the result is true, false, false
the part i don't understand is that the '&&' operator has a higher priority over '||' therfore it'll be evaluated first, but this is not what is happening.
for a strange reason '||' is evaluated first.
any ideas why?

Barkat Mardhani
Ranch Hand
Posts: 787
I do not understand how b and c are fales. They should be true....

Hanna Habashy
Ranch Hand
Posts: 532
hi:
boolean x = (a = true) || (b = true) && (c = true); this statement is equilvalent to: boolean x = (a = true) || ((b = true)) && (c = true));
In short ciruit operator OR, if the left side is true, the right side is not evaluated. In the previouse statement <a=true> then the right statement will never be evaluated, and b & c will stay false.

Steve Lovelace
Ranch Hand
Posts: 125
My understanding: operator precedene does not directly dictate order of evaluation - it dictates how operands get associated with operators. Evaluation proceeds from left to right. So as mentioned above
a + b * c
is treated as:
a + (b * c) and evaluation goes from left to right
as opposed to:
(b * c) + a
You can convince yourself of this with:

Vicken Karaoghlanian
Ranch Hand
Posts: 522
you are probably right Steve, however operator evaluation order sometimes gets really uncomfortable.
there is a lot of question regarding this matter in 'Dan Chisholm' exams, for instance...

this is evaluated as: a=(int)((1)+(++a + a++))
to evaluate an expression:
1) Use brackets to seperate operators with higher priorities.
2) always start evaluating from left to right.
for example: a = a + b * c;
1) a = a + (b * c)
2) start evaluating from L --> R

Steve Lovelace
Ranch Hand
Posts: 125
I think that is exactly what compilers are supposed to be doing.
HOWEVER, quoting from Kernighan & Ritchie "...writing code that depends on order of evaluation is a bad programming practive in any language."
HOWEVER, I believe Java specifies left-to-right order more rigorously than the C specification does: evaluation order