Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

boolean && || vs & | precedence

 
Louie van Bommel
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • 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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


See the table on this page for precedence...

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html
 
Mark Patrick
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Whoops. Sorry, I misread your question.
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • 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
  • 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))

=====
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic