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

# Operator precedence

Ranch Hand
Posts: 38
• Number of slices to send:
Optional 'thank-you' note:
Dan chisholm Mock exam

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);
}}

Ans) Prints: true,false,false

Explanation given first a= true is evaluated and since this is true the other part is not evaluated my question is when is left to right and operator precedence evaluated.

I was under the impression that since && has higher precedence over || the expressions
(b = true) && (c = true) is evaluated first followed by (a = true), can someone explain to me when the operands and precedence are evaluated

Also
int i=0, j = ++i + ((++i * ++i) % ++i) + ++i; The output of j = 8 eval as 1 + ( (2 *3) % 4) + 5
Can someone give me an example which justifies the statement that the prefix unary operator associates from Right to Left?

Ranch Hand
Posts: 1071
• Number of slices to send:
Optional 'thank-you' note:
On the first part:

(a = true) || (b = true) && (c = true)

Because the || is a short circut operator the compilier is able to determine that whatever is after the || is irrelevant and goes no farther. If what is before the || is false it will the evaluate the && before it evaluates the ||

Win jones
Ranch Hand
Posts: 38
• Number of slices to send:
Optional 'thank-you' note:
Thanks steve, I understand how || and && work - short -circuit part but I am unable to understand how the precedence of && and || applies to statements.

Steven Bell
Ranch Hand
Posts: 1071
• Number of slices to send:
Optional 'thank-you' note:
&& has a higher precidence over || so the
b && c
would be evaluated first and its result would be used
a || result
If the b && c were needed to understand the outcome.
however in this case that precidence is never taken into acount.
The question here isn't a matter of operator precidence it is an understanding of the short circut operation of conditional statements.

Ranch Hand
Posts: 79
• Number of slices to send:
Optional 'thank-you' note:
Before any operators operate, complete evaluation of operands takes place from left to right.

Win jones
Ranch Hand
Posts: 38
• Number of slices to send:
Optional 'thank-you' note:
Thanks guys, can you provide me some sample code where the prefix unary operator is evaluated based on Right to left operator precedence.

Win jones
Ranch Hand
Posts: 38
• Number of slices to send:
Optional 'thank-you' note:
Anand, can u elaborate on complete evaluation of operands takes place from left to right

Ranch Hand
Posts: 1272
• Number of slices to send:
Optional 'thank-you' note:
The problem is that precedence and associativity are linear approximations of how a compiler analyzes an expression in two dimensions. You'll notice the the Java Language Specification authors have explicitly refused to include an operator precedence table.

Below is an expression and its abstract syntax tree. As you can see, because && has a higher precedence, it groups its operands more tightly. So a || b && c is equivalent to a || ( b && c ). That does not mean that ( b && c ) is evaluated first. The controlling order-of-evaluation rule in the JLS is that the left operands of binary operators are always fully evaluated before the right operands. Finally, the operator is evauated. Notice how differently
( b = true ) && ( c = true ) || ( a = true ) would come out.

There are plenty of good books and web resources dealing with abstract syntax trees, if you want to understand this further.

a || b && c

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

can you provide me some sample code where the prefix unary operator is evaluated based on Right to left operator precedence.

Sure thing, pardner.

[ January 03, 2005: Message edited by: Mike Gershman ]
[ January 03, 2005: Message edited by: Mike Gershman ]