Forums Register Login

Dan's || && question

+Pie Number of slices to send: Send
Hello all,

I was doing Dan's mock question and got the following type question:

public class BoolTest {

static boolean a, b, c;
public static void main(String[] args) {
boolean y = (a = true) || (b = true) && (c = true);
System.out.println(a + " " + b + " " + c);
}
}

The answer is true false false.

He explains that LHS of || operator would be evaluated and the RHS of || operator gets short circuit.

My doubt is, && has greater precedence than ||. Then why didn't && operator evaluat first?? Had there been parenthesis in the RHS of || then it would have worked the way Dan explains it.

Why it is evaluating LHS of || first???

Regards.
+Pie Number of slices to send: Send
boolean y = (a = true) || (b = true) && (c = true);

As I understand it:
It attempts to short circuit BEFORE looking at precedence.

It sees that if the left hand expression is true, it need not evaluate anything else. It does so, then falls out true.
+Pie Number of slices to send: Send
"The || operator ... evaluates its right-hand operand only if the value of its left-hand operand is false. It is syntactically left-associative..."

SOURCE: http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#54532


"A left-associative operation is a non-associative operation that is conventionally evaluated from left to right..."

SOURCE: http://en.wikipedia.org/wiki/Associative
[ September 07, 2004: Message edited by: marc weber ]
+Pie Number of slices to send: Send
Put differently: don't confuse precedence with order of evaluation. They are different concepts. Evaluation generally goes from left to right; precedence determines how things are grouped.

- Peter
+Pie Number of slices to send: Send
Hi,

Thanks guys. Marc I didn't get your post. LHS of || is true, then why it checked RHS side. Secondly, for your 2nd comment - there is no point to see left association as || has higher precedence. LHS of || should have been checked before (which is true).

I am still confused???
+Pie Number of slices to send: Send
hi Peter,

My code was something like :



According to your kind post, it means that I should not look || && and evaluate op1. But looking at the above code, one must obey precedence rule.

Can you elaborate a little bit more?

Regards.
+Pie Number of slices to send: Send
 

Originally posted by Ankur Bhatt:
Marc I didn't get your post. LHS of || is true, then why it checked RHS side. Secondly, for your 2nd comment - there is no point to see left association as || has higher precedence.


Well, the operator || evaluates its right-hand operand only if the value of its left-hand operand is false. In this case, the LHS is true, so it does not evaluate the RHS (which is demonstrated in the output by b and c remaining false).

Evaluation from left to right only implies a "left-associative operation," and does not imply that || has higher precedence. Indeed, && apparently does have precedence over ||...

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

But I have to admit that I'm at a loss in understanding why && and || do not share the same precedence (like multiplication, division, and modulo). Given their short-circuiting behavior, how would we demonstrate that && does, in fact, have precedence over ||?


[ September 07, 2004: Message edited by: marc weber ]
+Pie Number of slices to send: Send
I think the && operator has more precedence than || means that the expression op1 || op2 && op3 will be evaluated as op1 || (op2 && op3)

If || would have more precedence then it would be (op1 || op2) && op3.

Now while evaluating op1 || (op2 && op3) first op1 part is checked. And as || is short (op2 && op3) part is evaluated only if necessary.

This is what precedence and order of execution means
Get me the mayor's office! I need to tell him about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 920 times.
Similar Threads
Dan's Mock Exam Q - Operators and Precedence
Dan's Tests | Question from 'Conditional Operator'
Dan's Tests | Question from 'Conditional Operator'
Dan's exam
Jxam short circuit question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:40:48.