• Post Reply Bookmark Topic Watch Topic
  • New Topic

&& and || operator precedence  RSS feed

 
Periakaruppan Thiagarajan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question from danchisholm site :

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

What is the output?.
What is the precedence of && and || operator?.
Which operator out of these two will be evaluated first?.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you run it and see? In fact it should be quite easy to work out. Hint: Remember the difference between the == operator and the = operator.

Note that as it stands, you cannot use that code to determine which operator has precedence. In fact, if you look carefully you will see that the output is not influenced at all by the use of the || and && operators here....
 
Chandrakanth
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically look at the precedence of the && and || operators
&& have more precedance than || operator

and

Here the question is based on the concept of short circuit operators(&& and ||)

ie., (operand1 && operand2) if operand1 evaluates to false, than operand2 is not evaluated.

so if u execute this question, u can see that only the first operand (a=true) being evaluated and remaining operands(b=true and c=true) being not evaluated
so the result printed is true, false and false
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Extra credit questions:

1. Why is a "short circuit" operator useful?

2. What is the difference between "||" and "|", and why does
one have "short circuit" behavior and the other doesn't?

3. What's the benefit of "&&" having higher precedence than "||"?
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Santa Maria:

2. What is the difference between "||" and "|", and why does
one have "short circuit" behavior and the other doesn't?


You answer the first part with the second part. I love tests like that.

As for "why" it's like that...
A. Because having two different operators do the same thig would be confusing.
B. Because the language designer(s) identified a need/desire for it.

How about this as a replacement question:

2. Give an example of when you would want to use the || (short circuit) operator and one of when you would use the | operator.
 
Rick Beaver
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Santa Maria:
Extra credit questions:

1. Why is a "short circuit" operator useful?


Consider the following two lines of code:



What would happen if myObject was null?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stuart Gray:
Why don't you run it and see? In fact it should be quite easy to work out. Hint: Remember the difference between the == operator and the = operator.

Note that as it stands, you cannot use that code to determine which operator has precedence. In fact, if you look carefully you will see that the output is not influenced at all by the use of the || and && operators here....


Actually, the || and && operators DO influence the output, although it isn't necessarily related to the precedence of these operators. However, it looks like others have already hinted at how these operators influence the output so I'll leave it at that.

Layne
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, Ryan. With regard to this question:
2. What is the difference between "||" and "|", and why does
one have "short circuit" behavior and the other doesn't?

I think you're suggesting that maybe the *only* difference is that one is a "short circuit operator" and the other isn't.

There's at least one other big difference between the two: one results in a boolean "true/false" (the output is exactly one bit); the other manipulates *all* of the bits in the integer.

What I was really getting at, though, is how disappointing it can be that some people just take classes and memorize a bunch of rules, without considering the "why's" of some of these rules. Or, more interesting, some alternative solutions that might be even better.

I'm not saying there are any such people in this group - I was just curious if I could pique anybody's curiousity. Like Rick's (great!) post. And I think (I hope!) I stimulated your thinking, too.

Your .. PSM
[ August 16, 2005: Message edited by: Paul Santa Maria ]
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Santa Maria:
Extra credit questions:

1. Why is a "short circuit" operator useful?

2. What is the difference between "||" and "|", and why does
one have "short circuit" behavior and the other doesn't?

3. What's the benefit of "&&" having higher precedence than "||"?


Short circuiting is faster:

if( flag && ... a dozen more checks ... )

Short circuiting is safer:

if( value != null && value.equals( "skooby doo" )) ...

The logical operators | and & do not short circuit in case you ever want to execute all of the code in a conditional statement:

if( enablePrimarySystem() & enableSecondarySystem() ) ...

The logical operators also provide bit-wise operations on numeric data types where as the relational operators are strictly for boolean expressions:

int a = x & y | z;

The different logical and relational operators are more like * versus + than * versus / so they have different precedence.

a || b && c is the same as a || (b && c)
[ August 17, 2005: Message edited by: Rick O'Shay ]
 
Periakaruppan Thiagarajan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I read a book written by khalid A Mughal. In that, he has mentioned && operator has higher precedence over || operator which means && will be evaluated before || operator
(a = true) || (b = true) && (c = true)
(a = true) || true && true
(a = true) || true // operands are evaluated from left to right
true || true

So, the answer should be (true, true, true) but the output was (true, false, false).
[ August 18, 2005: Message edited by: Periakaruppan Thiagarajan ]
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes u r right even i am getting true true true by theretical analysis but

practically the result is true false false..


even i need the xplanation..

Tx

Aneesh
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Precedence describes operator association not evaluation order. Precedence tables usually show evaluation order separately and with good reason.

a || b && c

Precedence: a || (b && c)

Evaluation order is left to right:

1. a
2 (b&& c)

The output should be true, false, false in your example because the first sub-expression short circuits the whole relation.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!