This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

Can anyone summarize the associativity rules for all the operators in Java? For example, I fail to understand why boolean c = a == b == true returns true(assuming a is equal to b). So if anyone can give me a good summarization of all the minute rules to be followed when evaluating operators in java would be of really help to me.

Regards, Jothi Shankar Kumar. S [ October 16, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!

To start with, Associativity rules are used to determine which operaor should be applied first if there are two operators of the same precedence.

So what all operators fall under the left associativity rule and what all fall under the right associativity rule?

Regards, Jothi Shankar Kumar. S

Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218

posted

0

With operators of the same precedence, an expression is evaluated from left to right, except for the ternary,unary and assignment operators which are grouped from right to left.

The operators *, /, and % are called the multiplicative operators. They have the same precedence and are syntactically left-associative (they group left-to-right).

The operators + and - are called the additive operators. They have the same precedence and are syntactically left-associative (they group left-to-right).

The shift operators include left shift <<, signed right shift >>, and unsigned right shift >>>; they are syntactically left-associative (they group left-to-right).

The equality operators are syntactically left-associative (they group left-to-right)

The bitwise operators and logical operators include the AND operator &, exclusive OR operator ^, and inclusive OR operator |. These operators have different precedence, with & having the highest precedence and | the lowest precedence. Each of these operators is syntactically left-associative (each groups left-to-right).

The &&, & is syntactically left-associative (it groups left-to-right).

The ||, | is syntactically left-associative (it groups left-to-right).

Expressions with unary operators group right-to-left.

The assignment operators are syntactically right-associative (they group right-to-left). [ October 17, 2006: Message edited by: Aniket Patil ]