• Post Reply Bookmark Topic Watch Topic
  • New Topic

Logical Operators - order of evaluation  RSS feed

 
Tom McCann
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you run this line of code it produces the output "true"

System.out.println(true | false & false)

In the absence of brackets, I would have thought it would have evaluated in left to right order, ie: "true or false" evaluated first, then the result of that (true) ANDed with false. Or to put it another way, equivalent to:

(true | false) & false

Can I take it from this that AND takes precedence over OR ?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. About midway down HERE
 
Tom McCann
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You had that figured out nicely. But complexities in order of precedence and using many languages at the same time a few years back led me to always make the order explicit with parens. I don't trust myself && various compilers && the next reader to all remember the precedence correctly. Well, the compilers are probably pretty reliable.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!