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)?
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?
Originally posted by Steve Morrow:
This just seems to cover precedence and I fully understand this. What I dont understand is associavity and how it differs from precedence.
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 ]