• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about boolean short circuit

 
Soniya Ahuja
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't understand the output of
System.out.println(false && true || true);
It is true.
I was under impression that 'false' is the priority for && operator.

Please explain.
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure if this is the answer - Please don't think that I am posting the question and answer all by myself. I want someone to explain the behind the scene point of view of the JVM

I think, the expression is evaluated until the result can be unambiguosly determined. Now, could someone please tell me how the compiler or the interpreter looks at this?
 
Antany Vasanth
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Soniya,

Java executes the your code in following order

result = false&&true --> always false
result = result||true --> always true;


Hence the result is true..

If you write the statement as like following code you will get false



result = true||true ---> always true where () took high priority
result = false && result ---> always false.


then the result is false.

Hope this will be helpfull for you.

Regards,
Antany
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Antany, thanks for replying. Actually, I wasn't wondering about the precedence.

I understand that using braces would mean assigning priority. Actually I didn't frame my question right

Let's take this example

boolean a = true;
boolean b = true;
boolean c = true;

System.out.println((a=false) && (b=false) && (c=false));
System.out.println(a+" "+b" "+c);

will result in

false
false true false

What do these results signify? Can't understand how the compiler looks at it.

System.out.println((a=false) && (b=false) || (c=false));
System.out.println(a+" "+b" "+c);

will result in

false
false false false

So I want to know why is && evaluated further beyond the second result
 
salvin francis
Bartender
Pie
Posts: 1212
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its compiler optimization that also plays an important role,





what do you think n would turn up here ?


Hint: In an "AND" expression, if one condition is false, the machine never bothers to even check the other conditions.

 
salvin francis
Bartender
Pie
Posts: 1212
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:check the other conditions.


when i said other conditions, i meant preceeding conditions.
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Salvin,

Thanks for posting but if you check the example I've written, it is evident that the compiler is evaluating expression beyond && but only when it sees that there's a || ahead. My question is what is exactly happening and why only selective expressions are being evaluated.

Thanks
 
Antany Vasanth
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello Soniya,


System.out.println((a=false) && (b=false) && (c=false));
System.out.println(a+" "+b" "+c);

will result in

false
false true false


Here the above code will not result false true false

It will return only false true true.

Java Executes the above statement as follows

first it will assing "false" value to "a" then it uses the value of "a" in condition.
so a=false. So the system dont consider the remining operations so the b and c value remains same.

In the second statement

System.out.println((a=false) && (b=false) || (c=false));
System.out.println(a+" "+b" "+c);


which will result in "false true false" not "false false false".

In the second statement "OR" is used.

Consider java executes the above statement as follows
a=false then it uses "a" in condition. a=false then the remaining statement wont execute until it finds "OR"

(a=false) && (b=false) leads false and the "b"(true) value remains same.

then the compiler took the result with operates or with c

false || (c=false) ==> false also the c value changed to false..

so the output will be false true false

Regards,
Antany
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soniya Ahuja wrote:.Actually, I wasn't wondering about the precedence.

...

So I want to know why is && evaluated further beyond the second result


You may not be wondering about precedence, but in order to understand what is going on, you'll need to understand precedence. The logical AND has higher precedence than the logical OR -- so this...



is seen by the compiler as ...



so... when the first false is encountered, the logical AND can only short circuit the second term (but not the third). Furthermore, the result of the AND is then used by the OR, and since it is false, it doesn't short circuit the third term.

Henry
 
salvin francis
Bartender
Pie
Posts: 1212
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin agrees with henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Hint: In an "AND" expression, if one condition is false, the machine never bothers to even check the other conditions.
That applies when you use && for AND.
 
salvin francis
Bartender
Pie
Posts: 1212
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i wrote

In an and expression


but it sounded too gramatically incorrect so i changed it.

of course i meant "&&" (without quotes )
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for replying. Sorry about the example I quoted. I had typed the wrong answers BTW, thanks for explaining the result of particularly the second expression.

One more question regarding the following

You may not be wondering about precedence, but in order to understand what is going on, you'll need to understand precedence. The logical AND has higher precedence than the logical OR -- so this...


I had read somewhere that in Java && and || take equal precedence.

But the sentence above suggests something different. I can't understand that if && takes higher precedence, why do we get the following output for the program below



output

true
a: true b: true c: true
 
salvin francis
Bartender
Pie
Posts: 1212
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
isnt

and different ?
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI salvin,
= and == are different. Only boolean assignments can be used in boolean expressions. Other assignements like integer assingments cannot be used. Boolean assignments can be used because the result of a boolean assignment is a boolean value.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soniya Ahuja wrote:I had read somewhere that in Java && and || take equal precedence.
I hope you are mistaken about having seen that. It is incorrect.

You will have to go through that confusing code you wrote with all the side-effects with a pencil and see how many of those assignments are ever executed.
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Soniya Ahuja wrote:I had read somewhere that in Java && and || take equal precedence.
I hope you are mistaken about having seen that. It is incorrect.

You will have to go through that confusing code you wrote with all the side-effects with a pencil and see how many of those assignments are ever executed.


HI Campbell, well I am confused . What side effects? Could you please elaborate? I was thinking that only one assignment has taken place - a=true. Well, if other assignments were to happen, wouldn't the values of the variables change? Oh, someone please explain it vividly
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The side-effects are (a = true) (b = false) and (c = false). As you have seen, some of those side-effects are not executed. If you write a line containing 4 pairs of round brackets(), three assignments, two member accesses .out and .println, a method call and two conditional operators, it is by no means easy to understand.

By the way: you are not using logical AND or logical OR. You are using conditional AND && and conditional OR ||. But conditional AND still has a higher precedence than conditional OR.

Anyway: you work from left to right:
  • Find the System class.
  • Find its out member
  • Find its println method
  • Find the expression in the () which will be the argument for println
  • Find the term in () which is a single term, and execute what is in those brackets.
  • Apply that value to the || short-circuit operator
  • Whatever is to the right of the || has only higher-precedence operators, so we do ??? with that sub-expression
  • Because the AND operator has a higher precedence than OR, what is on the right of || is regarded as a single unit. Now work out what the ??? should read.
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 20828
    75
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    By the way: you are not using logical AND or logical OR. You are using conditional AND && and conditional OR ||. But conditional AND still has a higher precedence than conditional OR.


    They actually are generally referred to as the logical AND (&&) and logical OR (||) -- even from C/C++ where the terms were borrowed. The non-short circuiting versions are generally referred to as the bitwise AND and bitwise OR.

    Hopefully, I am right here, or I have been mis-using these terms for more than two decades (back to my C days)...

    Henry
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 20828
    75
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Soniya Ahuja wrote:I was thinking that only one assignment has taken place - a=true. Well, if other assignments were to happen, wouldn't the values of the variables change? Oh, someone please explain it vividly


    Yes... only the first assignment happened. And yes... logical AND has higher precedence than logic OR !!

    Your example doesn't prove if the logical AND and logical OR has the same precedence or different precedences. Remember, order of evaluation, is not just determined by precedence.

    Henry
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47229
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So I looked it up in the Java™ Tutorials and didn't see "logical and" or "logical or" at all. In the JLS &^| are described as logical (as well as bitwise) and && || ?: as conditional. Obviously a lot of people use unofficial names.

    But have we worked out what ??? means, which I am sure is the same as what Henry is hinting at. Agree that the AND operators have higher precedences than the corresponding OR operators.
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 20828
    75
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Whew!! At least I am not the only one that uses these terms....

    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html


    Henry
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47229
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well if they can't keep consistent nomenclature from one page of the tutorial to the other

    Sorry
     
    salvin francis
    Bartender
    Pie
    Posts: 1212
    10
    Eclipse IDE Google Web Toolkit Java
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Soniya Ahuja wrote:HI salvin,
    = and == are different. Only boolean assignments can be used in boolean expressions. Other assignements like integer assingments cannot be used. Boolean assignments can be used because the result of a boolean assignment is a boolean value.


    I was merely remarking on your program:


    when you mention a=true, what do you expect to happen : assignment or a conditional check ?

    The output of the program is justified, but it seems you didnt intent it to be so.
     
    salvin francis
    Bartender
    Pie
    Posts: 1212
    10
    Eclipse IDE Google Web Toolkit Java
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You need to learn the following rule...

    In an "OR" ie "||" expression,

    if one condition is TRUE, rest conditions are NOT EVALUATED AT ALL



    This would print 0 not 1


    in an "AND" ie "&&" condition,
    If one condition is false, rest conditions are NOT EVALUATED AT ALL



    This would print 0 not 1


    another point , grouping:

    CONDITION1 || CONDITION2 && CONDITION3


    is treated as

    CONDITION1 || (CONDITION2 && CONDITION3)

    or

    CONDITION1 || GROUP1
    where GROUP1 = (CONDITION2 && CONDITION3)


    applyting the above logic to
    CONDITION1 || GROUP1

    CONDITION1 is TRUE so GROUP1 is not evaluated at all



     
    Soniya Ahuja
    Ranch Hand
    Posts: 83
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    salvin francis wrote:
    Soniya Ahuja wrote:HI salvin,
    = and == are different. Only boolean assignments can be used in boolean expressions. Other assignements like integer assingments cannot be used. Boolean assignments can be used because the result of a boolean assignment is a boolean value.


    I was merely remarking on your program:


    when you mention a=true, what do you expect to happen : assignment or a conditional check ?

    The output of the program is justified, but it seems you didnt intent it to be so.


    Hey guys, thanks again, but to be true I am still confused or rather more confused than before. Okay, starting with Salvin - I am using assignment and conditional check. The result of the assignment would give me the boolean condition and I wanted to analyse what's being evaluated by checking the results of these assignments. Hope what I've just said is correct

    Next, I think I am usign Logical ands and ors because the word conditional has left me befuddled

    Finally, what determines the order of evaluation if not precedence. I used to think that precedence helps us determine the order but now it seems all my basics were fallacious
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47229
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
  • The order of execution is left-to-right.
  • The parser splits the expression at its lowest-precedence operator which is ||.
  • The JVM evaluates the sub-expression on the left.
  • The value of the sub-expression on the left is ??? (guess: you only have two possibilities )
  • The value is applied to the || operator.
  • Maybe it is possible to work out the value of the whole expression at this stage
  • The JVM does ??? with the sub-expression on the right
  • ???
  • ???

  • Parser do not look for the highest-precedence operator; they look for the lowest-precedence operators and divide the expression into parts around those operators.
     
    Soniya Ahuja
    Ranch Hand
    Posts: 83
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a ton Campbell, things are finally starting to stick now

    So the split takes place at the low precedence operator which in this case is the OR operator. Since the value of the sub expression to the left is true, ORing this with the rest would return true and hence the rest of the expression is ignored. Is that correct?

    Okay now this finally makes sense to me Thanks everyone
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 47229
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes. Because the first half of the expression before the || (1st ???) is true, the JVM does nothing with the rest of it.

    So the other ???s evalute to "nothing".
     
    salvin francis
    Bartender
    Pie
    Posts: 1212
    10
    Eclipse IDE Google Web Toolkit Java
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    I didnt intend to confuse you at all.
    I am glad you have sorted things out now.

    as an advice, dont mix the two (conditions + assignment)
     
    Soniya Ahuja
    Ranch Hand
    Posts: 83
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hey Salvin, all the inputs here have helped me a lot. All confusions have evaded me ;)

    Thanks
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic