File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Help with complicated boolean expresion using &,&&,| and || Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Help with complicated boolean expresion using &,&&,| and ||" Watch "Help with complicated boolean expresion using &,&&,| and ||" New topic
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
    
    4
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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Help with complicated boolean expresion using &,&&,| and ||
 
Similar Threads
Wht would b the output plz explain
Satou kurinosuke i have one more question for you
short circuit operators question
Conditional Operators (try this)
shortcut circuit Q