This week's book giveaway is in the Servlets forum. We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line! See this thread for details.

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

&& 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 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!

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))