File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Logical operators precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Logical operators precedence" Watch "Logical operators precedence" New topic

Logical operators precedence

AJ Brown

Joined: Mar 30, 2004
Posts: 17
Can someone please explain how to come up with this answer? It's from the Sierra and Bates book (page 199), but I still do not understand when looking at the answer explanation in the book. I guess I need someone to explain the order of evaluation of lines 6 and 8 in detail. Thanks!!
Given the following:
1. class SSBool {
2. public static void main(String [] args) {
3. boolean b1 = true;
4. boolean b2 = false;
5. boolean b3 = true;
6. if ( b1 & b2 | b2 & b3 | b2 )
7. System.out.println("ok ");
8. if ( b1 & b2 | b2 & b3 | b2 | b1 )
9. System.out.println("dokey");
10. }
11. }
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
So, what's the answer you're looking for help explaining?
I'll assume it's the output of the program.
Then, which part of the program are you looking for help explaining?
I'll assume it's the compound boolean conditions used in the two if statements.
You can think of the AND operators as binding operands more tightly than the associated OR operators. In other words, the AND operators are evaluated before the OR operators.
If it helps you, you could add parentheses to the expression to help clarify the order that things are evaluated.
So, for the first if statement, you could think of b1 & b2 | b2 & b3 | b2 as (b1 & b2) | (b2 & b3) | b2, and then hopefully it becomes clear that the expression evaluates to FALSE OR FALSE OR FALSE, which is FALSE, and so "ok " is not displayed to the console.
Is the evaluation of the second if statement any clearer?
Now, back on the subject of asking a good question. When asking folks a question, taking a minute to first explain things to your Cardboard Analyst and to think about Asking a Good Question is time well spent.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
AJ Brown

Joined: Mar 30, 2004
Posts: 17
Adding the ( ) helps me understand
so the line....
if ( b1 & b2 | b2 & b3 | b2 | b1 )
should be read as
(true & false) | (false & true) | false | true
goes to next step...
false | false | false | true
so this is true then
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
I had the same problem as you. But after a while, I figure out that I wasn't reading the explanations carefully enough. Either that or I wasn't really trying to think.
Anyway, &&, & operators have higher precedence than || or | operators. Meaning to say, you evaluate the operands that are Anded together first, then ORing the rest.
No parentheses are needed, if you follow their precedence order. However, putting the brackets in sure help to improve your code clarity! But then again, in the exams, they're out to confuse us!

SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
I agree. Here's the link:
subject: Logical operators precedence
It's not a secret anymore!