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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Logical operator precedence

Ranch Hand
Posts: 124
Hi guys,
Sorry one more for today

I am reading a tutorial on logical operators and it states "The associativity of the &&, ||, &, | and ^ operators is from left to right." It also includes a diagram of these operators in the order above with an arrow moving from left to right.

This does not seem to match the precedence rules I have found on a number of web sites e.g. http://www.uni-bonn.de/~manfear/javaoperators.php

I think the order of precedence should be &, ^, |, &&, ||
Can someone verify this for me (and that the tutorial is incorrect)?

Cheers,
John

Ranch Hand
Posts: 91
I think you're confusing associativity with precedence. They're not the same thing.

John Ryan
Ranch Hand
Posts: 124

Originally posted by Kristin Stromberg:
I think you're confusing associativity with precedence. They're not the same thing.

Okay thanks Kirstin. can anyone explain to me what associatity means with respect to logical operators. also what is the difference between associativity and prededence?

Cheers,
John

Ranch Hand
Posts: 657
Operator Precedence

John Ryan
Ranch Hand
Posts: 124

Originally posted by Steve Morrow:
Operator Precedence

Hi Steve,
This just seems to cover precedence and I fully understand this. What I dont understand is associavity and how it differs from precedence.

Cheers,
John

Kristin Stromberg
Ranch Hand
Posts: 91
This might help... it's from A Programmer's Guide to Certification

Precedence rules are used to determine which operator should be applied first if there are two operators with different precedence, and these follow each other in the expression. In such a case, the operator with the highest precedence is applied first.

2 + 3 * 4 is evaluated as 2 + (3 * 4) (with the result 14) since * has higher precedence than +.

Associativity rules are used to determine which operator should be applied first if there are two operators with the same precedence, and these follow each other in the expression.

Left associativity implies grouping from left to right:

1 + 2 - 3 is interpreted as ((1 + 2) - 3), since the binary operators + and - both have same precedence and left associativity.

Right associativity implies grouping from right to left:

- - 4 is interpreted as (- (- 4)) (with the result 4), since the unary operator - has right associativity.

The precedence and associativity rules together determine the evaluation order of the operators.

[ June 21, 2005: Message edited by: Kristin Stromberg ]

John Ryan
Ranch Hand
Posts: 124
Thanks Kristin. This fully explains the difference