• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with complicated boolean expresion using &,&&,| and ||

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Justin
[ May 25, 2008: Message edited by: Campbell Ritchie ]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic