| Author |
boolean && || vs & | precedence
|
Louie van Bommel
Ranch Hand
Joined: Aug 17, 2004
Posts: 76
|
|
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
Joined: Aug 31, 2004
Posts: 11343
|
|
See the table on this page for precedence... http://java.sun.com/docs/books/tutorial/java/nutsandbolts/expressions.html
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Mark Patrick
Ranch Hand
Joined: Feb 22, 2004
Posts: 51
|
|
&& 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<br />SCJP 1.4
|
 |
Mark Patrick
Ranch Hand
Joined: Feb 22, 2004
Posts: 51
|
|
|
Whoops. Sorry, I misread your question.
|
 |
Manikandan Jayaraman
Ranch Hand
Joined: Sep 15, 2004
Posts: 225
|
|
============ 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!
|
Regards,<br />Mani<br />SCJP 1.4 (95%)<br />SCWCD 1.4 (94%)
|
 |
Manikandan Jayaraman
Ranch Hand
Joined: Sep 15, 2004
Posts: 225
|
|
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)) =====
|
 |
 |
|
|
subject: boolean && || vs & | precedence
|
|
|