• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Dan's || && question

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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 ]
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bharat Roy
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Bharat Roy
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic