programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Boolean Logic

Greenhorn
Posts: 2
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
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
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
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
Jesper de Jong wrote:. . . &&, which has a higher precedence than |. . . .
Isn't it the other way round? & > ^ > | ? && > ||