*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Evaluation Order. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Evaluation Order." Watch "Evaluation Order." New topic
Author

Evaluation Order.

Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
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?


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
I do not understand how b and c are fales. They should be true....
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
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.


SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Steve Lovelace
Ranch Hand

Joined: Sep 03, 2003
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:


The Inner that is named is not the true Inner.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
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++))
Steve, what do you think about this:
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

Joined: Sep 03, 2003
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Evaluation Order.
 
Similar Threads
Assignment Q
doubt in || and &&
Conditional Operators
Operator Precedence
Conditional And-Or