• Post Reply Bookmark Topic Watch Topic
  • New Topic

Boolean Logic  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote this bit of code to better understand boolean logic and operator precedence:



The result:


The first result makes sense to me - one() evaluates to 'false', no need to call two(); three() must be called to determine the result of the expression.

The second result, not so much.... Due to operator precedence (http://www.uni-bonn.de/~manfear/javaoperators.php) and the '|' requiring that both operands must be evaluated - I would have figured the functions would be called as follows: two(), three(), one() resulting in 'false'.

I am trying to sort this stuff out in my head as I am preparing for the SCJP... just thought I would share
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

In the second example (line 7) you are still using &&, which has a higher precedence than |.

So Java will first evaluate: one() && two()

As you already noticed yourself, && is a short-circuiting operator, so because one() returns true it doesn't need to call two().

Then the result of one() && two() is used in the second part: true | three()

Because | is not short-circuiting, it will call three() in this second example (even though it is already known that the result is going to be: true).

Using a short-circuiting operator anywhere in a long expression doesn't mean that Java will automatically evaluate everything in the whole expression. The (non) short-circuiting only applies to the two arguments of that single operator.
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper is bit off, because three() isn't even called. The reason is the shortcut operator.

In your first example, the result is the same as (one() && two()) || three(). one() is false, so one() and two() will also be false, and two() is not evaluated. Because one() && two() is false there is need to evaluate three().

In the second example, the result is the same as one() && (two() | three()). one() is false, so the shortcut operator already knows that the entire result is false, no matter what two() | three() yields. It will therefore not even attempt to evaluate it.

Change the return values of all methods to true and see that in the second example all three methods are called, in the first example only one and two.
 
Mike Scammacca
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper & Rob - thanks for the response.


In the second example, the result is the same as one() && (two() | three()). one() is false, so the shortcut operator already knows that the entire result is false, no matter what two() | three() yields. It will therefore not even attempt to evaluate it.


This made it 'click' for me, thanks!
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:. . . &&, which has a higher precedence than |. . . .
Isn't it the other way round? & > ^ > | ? && > ||
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!