• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

boolean && || vs & | precedence

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could someone show me an example where precedence of operators actually make a difference in expressions that involve boolean-only expressions using
1. && with ||
and
2. & with |

I remember seeing something in the "Nutshell" book about operator precedence regarding these operators. I don't think K&B (K&&B )'s book talks much about precedence using boolean expressions.

My current belief is that with expressions involving only the short-curcuit operators, the expression is evaluated left to right no matter what. And I don't have a CLUE what the order of evaluation is on expressions involving only & and
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


See the table on this page for precedence...

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
&& and || are short circuit operators. That means that as soon as the condition can be positively evaluated, all other conditions will NOT be checked. However, & and | will cause every condition to be checked no matter what results from previous condition checks. Therefore, check out how the value of 'i' is effected:

 
Mark Patrick
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoops. Sorry, I misread your question.
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
============
Mark wrote:
Whoops. Sorry, I misread your question.
============

Again reopening a old topic .... but still, I feel the example suggested by Mark, had wrong comments ... The solution written, through the comments accompanying every line, were wrong .... Was that you tried to convey, in this thread by your above statement ?

And another reason for why I reopened this thread is, we never discussed here about the precedence , if an expression has a mix of || , &&, & , |.

Can someone give an example covering all of these? Btw. I will also give it a try!
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ... Tried to figure out, how precedence act between Bit-Wise and Short-Circuit Operators. Please confirm on my observations, so that I can ascertain and generalise my concepts. Thanks.

=====

System.out.println( true | true && false );
//bitwise OR first considered, resulting in false.

System.out.println( true || true & false );
//bitwise AND first considered, resulting in true.

System.out.println( false & false || true );
//bitwise AND first considered, resulting in true.

System.out.println( false && false | true );
//bitwise OR first considered, resulting in false.

//The above demonstrates bitwise op. have more precedence to short-circuit operators.

// For the below, what I feel is first solve the bitwise portions and then
// evaluvate the remaining short circuit op. See below ...

System.out.println( true | true && false || false & false || true);
//true
// The above can be mentioned as (true | true) && false || (false & false) || true

System.out.println( true | true && false && false & false || true);
//true
// The above can be mentioned as (true | true) && false && (false & false) || true

System.out.println( true | true && false || false | true && false);
//false
// The above can be mentioned as (true | true) && false || (false | true) && false

System.out.println( true | true && false || false | true & true);
//true
// The above can be mentioned as (true | true) && false || (false | (true & true))

=====
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic