Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!

# Precedence of short-cicuiting AND over short-circuiting OR

Andras Toth
Greenhorn
Posts: 4
In the Mala Gupta book, there is the following example:

And the following is stated about precedence:

The expression (a >= 99 || a <= 33 && b == 10) has three operands together with the OR (||) and AND (&&) short-circuit operators. Because the short-circuit operator AND has higher operator precedence than the short-circuit operator OR, the expression is evaluated as follows: (a >= 99 || (a <= 33 && b == 10))
Evaluation of the preceding expression starts with the evaluation of (a <= 33 && b == 10).

However, it seems to me that it is a>99 which would be really evaluated first. Consider the following code:

This will print out the following:

So it seems to me, that the order is not the one stated in the book. However, I also found in that "AND" operator does precede "OR" operator, so the statement in the book should be right. (Besides, this problem is not mentioned in the errata of the book.)

Can somebody explain me why in my code snippet does not behave as stated in the book? Thanks in advance!

Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
• 1
To me, && having higher precedence, means these two are equivalent:

This would mean that f1 still runs first. And if f1 is true, there is no need for either f2 or f3 to run.

Matthew Brown
Bartender
Posts: 4568
9
There's a difference between precedence and order of evaluation. Java expressions are evaluated left-to-right, regardless of precedence. Unless the expressions have side-effects it doesn't matter, but this is an example where they do have side effects so you can see what is happening.

So the phasing in the book is a bit misleading. f1() is evaluated first, but the overall value is as if you combined the results of f2() and f3(), and then combined that with f1().

Andras Toth
Greenhorn
Posts: 4
Hi Jeanne, Matthew: Thanks for the clarification, now the difference between order of evaluation and precedence order is clear to me.

Prathima gaitonde
Ranch Hand
Posts: 130
3
I was about to ask the same question. I saw this code in enthuware. The question has been discussed there too, which is not clear to me. Its like this.

What will be the out put of following program?

The correct answer is true false false.

Now my questions are.

1> Is && and || has the same precedence?
2> If && has more precedence than ||, which is what according to Java Language Specifications, then Why is the out put not true true true?
3> I am still not getting the concept of order of evaluation and order of precedence.

Roel De Nijs
Sheriff
Posts: 10662
144
Operator precedence and order of execution is probably one of the topics with the most questions on this forum. Here is another great thread with a bunch of code examples.

Roel De Nijs
Sheriff
Posts: 10662
144
Prathima gaitonde wrote:The correct answer is true false false.

That's wrong! The correct answer is Compilation fails. You can't access instance fields from a static method.

Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3819
10
Roel De Nijs wrote:
Prathima gaitonde wrote:The correct answer is true false false.

That's wrong! The correct answer is Compilation fails. You can't access instance fields from a static method.

I think Prathima has misquoted the question. I searched for the question and I couldn't find any that has this problem. The closest that I found was 2.1156 and it uses static fields. I also verified that the given answer is correct.

@Prathima - please do let me know the question id of the question if you see something wrong with that question.

thank you,
Paul.

Prathima gaitonde
Ranch Hand
Posts: 130
3
Sorry @Roel and @Paul. My mistake the question has the static fields boolean a,b,c. I don't remember the exact id of it. I will look for it, and let you know @paul. @Roel, the code has no compilation issue. It will print true, false, false. I am just not getting why is it so? Thanks.

Roel De Nijs
Sheriff
Posts: 10662
144
Prathima gaitonde wrote:@Roel, the code has no compilation issue. It will print true, false, false. I am just not getting why is it so?

This thread has exactly the same question and an excellent explanation (of me ). So have a look and carefully read this thread. If you still have doubts, don't hesitate to ask them here.

Hope it helps!
Kind regards,
Roel