Results 1 to 7 of 7
 10262011, 07:22 PM #1Member
 Join Date
 Oct 2011
 Location
 New Jersey
 Posts
 44
 Rep Power
 0
Logic Expression ... possible bug for short circuit boolean operator?
I just came across some thing that's supposed to be fundamental but it puzzled me a lot.
It's the evaluation of the logic/boolean expression : A or B or C and D.
if this is written in java shortcircuit boolean operators: A  B  C && D
if D is false, by right the whole expression is false.
However if any of A, B, is true, java shortcircuit boolean operator seems to stop after the first  is true. I think by right, the more correct operation/practice is to skip all the parallel ORs, and jump to the parallel ANDs, sees D is false, and return whole expression value as false.
Please feel free to comment on the following test code:
==================================================
public class testLogic {
public static void main(String[] args)
{
boolean a = true;
boolean b = true;
boolean c = false;
boolean d = false;
System.out.println("(a  b  c) && d = " +( (a  b  c) && d));
System.out.println("a  b  c && d = " +( a  b  c && d));
System.out.println("a  b  c & d = " +( a  b  c && d));
}
}
================================================== execution
Test> java testLogic
(a  b  c) && d = false
a  b  c && d = true
a  b  c & d = false
 10262011, 07:43 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,389
 Blog Entries
 7
 Rep Power
 25
Re: Logic Expression ... possible bug for short circuit boolean operator?
The operator precedence of the && operator is higher than the  operator precedence. So a  b  c && d is evaluated as a  b  (c && d); short circuiting stops the evaluation (which is strictly from left to right) if a happens to be true. If none of a and b are true then c && d must be true to make the entire expression true. The && operator has lower precedence than the  (single vertical bar) operator, so a  b  c && d is evaluated as (a  b  c ) && d.
kind regards,
JosLast edited by JosAH; 10262011 at 07:46 PM.
The only person who got everything done by Friday was Robinson Crusoe.
 10262011, 08:00 PM #3Member
 Join Date
 Oct 2011
 Location
 New Jersey
 Posts
 44
 Rep Power
 0
Re: Logic Expression ... possible bug for short circuit boolean operator?
Thank you Jobs! That's very clear and helpful explanation.
Just 1 more question.. Is this order of precedence by design? would grouping by brackets increase the cost of evaluation?
It seems a trade off between cost and clarity here..
 10262011, 08:13 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,389
 Blog Entries
 7
 Rep Power
 25
Re: Logic Expression ... possible bug for short circuit boolean operator?
Nope, brackets in expressions don't cost anything during runtime, e.g. (((((1+(((((2)))))))))) is the same as 1+2; the compiler might have to take a few extra steps (depending on the way the expression is parsed) but the result in compiled code is the same. Brackets in expressions can change the order of evaluation though, e.g. 2*(3+4) is evaluated in a different order than the expression 2*3+4.
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 10272011, 04:21 PM #5Member
 Join Date
 Oct 2011
 Location
 New Jersey
 Posts
 44
 Rep Power
 0
Re: Logic Expression ... possible bug for short circuit boolean operator?
Thanks so much Jos for taking time to reply!
 10272011, 04:30 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,389
 Blog Entries
 7
 Rep Power
 25
 10272011, 06:40 PM #7
Re: Logic Expression ... possible bug for short circuit boolean operator?
JosAH beat me to the answer, but I'll chip in that using lots of parenthesis is a good practice. In Eclipse, you can set code cleanup to add them automatically. I HATE having to find a bug in code where the programmer relied on precedence, rather than using parenthesis. I have to parse every expression, knowing one of them is wrong.
The Java Tutorial. Read it.
Similar Threads

Boolean Logic
By DaveC in forum New To JavaReplies: 6Last Post: 06152011, 05:11 AM 
[SOLVED] Boolean Expression Evaluation Framework
By priyanka.dandekar in forum Advanced JavaReplies: 8Last Post: 03272010, 03:35 PM 
operator  cant be applied to OlimpicFrog, boolean
By darkblue24 in forum New To JavaReplies: 2Last Post: 02162010, 01:37 AM 
[SOLVED] need help with logic operator
By auralius in forum New To JavaReplies: 10Last Post: 12252008, 11:01 PM 
Boolean Expression
By ritwik07 in forum New To JavaReplies: 3Last Post: 07112007, 04:11 AM
Bookmarks