File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's || && question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic
Author

Dan's || && question

Bharat Roy
Ranch Hand

Joined: Jul 01, 2004
Posts: 156
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.
Tom Tolman
Ranch Hand

Joined: Sep 02, 2004
Posts: 83
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.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Jul 01, 2004
Posts: 156
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

Joined: Jul 01, 2004
Posts: 156
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

Joined: Aug 31, 2004
Posts: 11343

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 ]
Oneal Shaha
Ranch Hand

Joined: Aug 11, 2004
Posts: 98
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


-Oneal
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dan's || && question