Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Logical operator precedence

 
John Zoetebier
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to "Just Java, third edition, page 128" from Peter van der Linden has operator && precedence 4 and operator || precedence number 3.
This would imply that operator && has higher precedence than operator ||
I did a mock test of JQPlus and the explanation said there is no precedence of logical operators.
I have not been able to find a statement that indicates a difference in precedence between these logical operators (&& and ||).
Does anybody know the answer ?
 
Zbig Gala
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
boolean b = true || false && false;
System.out.println(b);
If && has higher precedence the result will be true.
Did it help?.
 
Karin Paola Illuminate
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the following code? Is it possible to test precedence with it?
I expected that it would print something different than "method 1".

[ April 13, 2003: Message edited by: Karin Paola Illuminate ]
 
Zbig Gala
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thanks for your feedback
The result is
----
method1
true
----
right?
This is because left-hand (method1) operand of || operator is guaranteed to be evaluated prior to the right-hand one.
Precedence rules decide here only that the expression (method2 && method3) forms an operand of the operation || (which doesn't say when this operand will be evaluated if at all)
Zbig
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at a recent operator precedence thread in which Jim Yingst helped shed some useful light on this very subject.
 
John Zoetebier
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the thread suggested by Dirk Schreckmann most helpfull.
What Jim Yingst suggests is true for all practical purposes.
==>
&& has higher precedence than ||, but the expression still evaluates left to right, as short-circuit operations require that the left-hand side of an expression be evaluated before the right. Look at it this way:
boolean x = (a = true) || (b = true) && (c = true);
is (thanks to precedence) exactly equivalent to
boolean x = (a = true) || ((b = true) && (c = true));
==>
My big question though is: can anybody show me an example of a boolean statement that has a different solution when:
1. && has precedence over ||
2. && and || have equal precedence.
I have all combinations for 3 and 4 operands in:
boolean b;
b = c1 || c2 && c3
b = c1 && c2 || c3 && c4;
For example lets take this example.
b = true || false && false
1. Same presedence rule
We start at the left and get condition true.
Apply shortcuit || => skip rest of conditions => true
2. && higher precedence
(false && false) => false
true || false => true
In both case same result.
You can try out all combinations for 3 operand and will see that the result is the same.
I have tried the following statement with 4 operands.
b = c1 && c2 || c3 && c4;
Try out all possible combinations and check result for both rules.
Again: same result for both rules.
The rule of Jim applies when the operand is an expression or assignment.
What I have been able to figure out is that the algoritm applied by Java for processing logical statements is:
1. Group operands according to precedence
2. Evalate statement from left to right
3. Evaluate expressions or assigments while processing from left to right
The order of the rules is as above.
Expression evaluation comes AFTER precedence rules.
In case of the left operand of || being true, the rest of the statement is skipped, regardless of precedence rules !
 
John Zoetebier
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is a program that shows how Java processes expressions in a boolean statement.
For example you can change the argument in m1:
1 results in true
0 results in false
Just see for yourself what happens if the left operand results in true or false.
The test results confirm the rules I mentioned in an earlier posting.
 
Monisha Talwar
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,
I tried the code, and I got the result as:

Q4: m1()
1: true
===
Q4: m1()
Q4: m3()
1: true
Done
Press any key to continue...

I am still confused about this
The first statement is:
b1 = m1(1) || m2(1) && m3(0)
1. m1(1) is called, and true is returned.
2. The || is short-circuited because LHS is true.
3. So it skips the m2(1) && m3(0) part.
I got that part (or I think I did!)
Now the second statement is:
b1 = m1(0) && m2(1) || m3(1);
1. m1(0) is called, it is false
2. So why is the && operator not getting short circuited directly at this point?
It skips the m2(1) and goes ahead to proceed m3(1)...Why is it going ahead to preceed with the rest of the statement?
If the rule is that the whole statement has to be processed, then why did the first statement not do that?
I am really confused....please help...I am taking the exam in 2 days....
 
Monisha Talwar
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, after working on it a little more I got it...
The first statement is:
b1 = m1(1) || m2(1) && m3(0)
Its evaluated as :
m1(1) || (m2(1) && m3(0))
1. m1(1) is called, and true is returned.
2. The || is short-circuited because LHS is true.
3. So it skips the RHS - entire (m2(1) && m3(0)) part.
Now the second statement is:
b1 = m1(0) && m2(1) || m3(1);
Its evaluated as :
b1 = (m1(0) && m2(1)) || m3(1);
1. m1(0) is called, it is false
2. So && short-circuits and m2(1)is skipped.
3. Now (m1(0) && m2(1)) evaluate to false.
4. So || goes ahead and checks m3(1).
Phew!!...I scared my self for a minute there
Monisha
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Monisha, that concords with:

I would say that a || operator short-circuits the whole of the remaining expression, providing no parentheses comes into the sceene. The point is that the && operator does not behave exactly equal. A false value to the left of && will short-circuit only sucessive && operators to its right, untill the first || operator on its right.

In this
thread
 
John Zoetebier
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monisha says:

Now the second statement is:
b1 = m1(0) && m2(1) || m3(1);
1. m1(0) is called, it is false
2. So why is the && operator not getting short circuited directly at this point?
It skips the m2(1) and goes ahead to proceed m3(1)...Why is it going ahead to preceed with the rest of the statement?
If the rule is that the whole statement has to be processed, then why did the first statement not do that?
I am really confused....please help...I am taking the exam in 2 days....

Sorry, for my late reply.
Somehow I do not get email for postings.
Anyway, here is my explanationation.
The statements is:
b1 = m1(0) && m2(1) || m3(1);
Rule 1: group operands according to precedence
b1 = (m1(0) && m2(1)) || m3(1);
Rule 2: process from left to right
Rule 3: evaluate expressions when needed
This give us:
m1(0) is false =>
(m1(0) && m2(1)) is false
Note that m2(1) is NOT evaluated !
This give us:
b = (false) || m3(1);
Evaluate m3(1) => true
This gives us:
b = (false) || true
b = true
The 3 rules I described before, show how the compiler processes boolean statements.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic