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
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# && and || operator precedence

Ranch Hand
Posts: 65
• Number of slices to send:
Optional 'thank-you' note:
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?.

Ranch Hand
Posts: 410
• Number of slices to send:
Optional 'thank-you' note:
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....

Ranch Hand
Posts: 60
• Number of slices to send:
Optional 'thank-you' note:
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

Ranch Hand
Posts: 236
• Number of slices to send:
Optional 'thank-you' note:
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 "||"?

Bartender
Posts: 1205
22
• Number of slices to send:
Optional 'thank-you' note:

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.

2. Give an example of when you would want to use the || (short circuit) operator and one of when you would use the | operator.

Ranch Hand
Posts: 464
• Number of slices to send:
Optional 'thank-you' note:

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?

Ranch Hand
Posts: 3061
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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.

[ August 16, 2005: Message edited by: Paul Santa Maria ]

Ranch Hand
Posts: 531
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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 ]

Ranch Hand
Posts: 980
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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.