programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# order of precedence in logical operators

Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
boolean val = true | false ^ true;

I don't understand why this evaluates to true.  Do the "|" and "^" have the same level of precedence?  I think the answer is yes.  So if the order of precedence is the same, then the expression is evaluated left to right?  So the statement is equivalenet to

boolean val = (true | false)  ^ true;

Which should evaluate to false.  I don't understand why the false ^ true portion of the expression is evaluated before the  true | portion of the expression.

Can someone please clarify what I am misunderstanding in this expression?

Rancher
Posts: 188
7
• Number of slices to send:
Optional 'thank-you' note:

Robin Z. Clark wrote:  Do the "|" and "^" have the same level of precedence?

No.

Sheriff
Posts: 27456
88
• Number of slices to send:
Optional 'thank-you' note:
Or following the logical path that Robin already started down:

"If the order of precedence is the same, then the result would be false. But it isn't. So it follows that the order of precedence isn't the same."

Sheriff
Posts: 7123
184
• Number of slices to send:
Optional 'thank-you' note:
Here's a nice table of operators and their precedence:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

Robin Z. Clark
Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
Thank you!  That is a good table.  It is different from the table I am looking at in OCA Study Guide by Jeanne Boyarsky and Scott Selikoff on page 53.  I have not found any errors in this book so I must be misunderstanding their table.

Marshal
Posts: 76873
366
• Number of slices to send:
Optional 'thank-you' note:
Please tell us how that table differs rom that in Boyarsky and Selikoff.

Robin Z. Clark
Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
I'll try to include a picture.
15779625718234938210629061946752.jpg

Robin Z. Clark
Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
Here is the first part of the table.
15779626665284204010848945949776.jpg

Robin Z. Clark
Greenhorn
Posts: 29
• Number of slices to send:
Optional 'thank-you' note:
When it says "If two operators have the same level of precedence, then Java guarantees left-to-right evaluation" I interpreted that to mean that the expression would be evaluated left to right.  Maybe what is meant is that the precedence of the operators in the table should be ordered left to right?  That seems very very confusing to me.

Operator Precedence table posted by Knute

The table that Knute referenced clearly shows that the ^ operator has higher precedence than the "|" operator.  That makes perfect sense.