| Author |
Help with complicated boolean expresion using &,&&,| and ||
|
justin mat
Greenhorn
Joined: May 25, 2008
Posts: 1
|
|
Hi Can some experts tell me how i should interpret the following to get the correct result? Are they any precedent order in reading a combination of | || & and && boolean dd =false||true|false&&true&false&true|true&&false||true&&true; Best Regards, Justin
|
 |
Marco Ehrentreich
best scout
Bartender
Joined: Mar 07, 2007
Posts: 1220
|
|
Hi justin, here's an overview of the Java operators in general and the precedence rules: operators For conditional operators like && or || there's a special feature called short-circuiting or short circuit evaluation. This means that the expression is only evaluated until the result is clear. For example for "A && B" it's sufficient for the whole expression to be false if A is false. Therefore expression B doesn't even get evaluated if A is false. And for your question how you should evaluate these expression "by hand" it's probably a good idea to make the precedence rules explicit by inserting parentheses accordingly Marco
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32595
|
|
Welcome to JavaRanch, Justin  [ May 25, 2008: Message edited by: Campbell Ritchie ]
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
Originally posted by Marco Ehrentreich: it's probably a good idea to make the precedence rules explicit by inserting parentheses accordingly
In fact, when writing code for yourself, ALWAYS add parentheses if the expression will become this complex. It will help you when reading your own code later on. I myself hardly ever really rely on operator precedence, but use parentheses to make sure it will work. That way, programmers from other languages, with a possibly other precedence, can read my code just fine too.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: Help with complicated boolean expresion using &,&&,| and ||
|
|
|