• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Mac OS X IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



No.
 
Sheriff
Posts: 27456
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell us how that table differs rom that in Boyarsky and Selikoff.
 
Robin Z. Clark
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll try to include a picture.
15779625718234938210629061946752.jpg
[Thumbnail for 15779625718234938210629061946752.jpg]
 
Robin Z. Clark
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the first part of the table.
15779626665284204010848945949776.jpg
[Thumbnail for 15779626665284204010848945949776.jpg]
 
Robin Z. Clark
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

I posted a topic in the book's errata thread.  Errata thread

 
Rancher
Posts: 4893
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you considering the parenthesis?
Whoops missed first expression w/o ()s
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic