• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Summarization required

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

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 ]
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the precedence order as given in The Java Programming Language: 2nd Edition

OPERATOR PRECEDENCE
P-ost-fix Operators [] . (parameters) expression++ expresion--
P-efix Unary Operators ++expression --expression
O-bject creation and cast new (type)
M-ultiplication * / %
A-ddition + -
S-hift << >> >>>
R-elational Operators < <= > >= instanceof
E-quality Operators == !=
B-itwise/boolean AND &
-itwise/boolean XOR ^
-itwise/boolean OR |
L-ogical AND &&
-ogical OR | |
C-onditional Operator ?:
A-ssignment = += -= *= /= %= <<= >>= >>>= &= ^= !=
[ October 16, 2006: Message edited by: Aniket Patil ]
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Above,

I did not ask for the precedence rules but rather on how to associate multiple operators? Please anyone can get this for me?

Regards,
Jothi Shankar Kumar. S
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

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
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Above,

Your definition lacks essence. Can you please elaborate. I guees you have missed something.

Regards,
Jothi Shankar Kumar. S
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aniket,

Thanks for the explanation. By the way, how to remember these precedence rules?Is there any logic behind it so that we can remember it always?

Regards,
Jothi Shankar Kumar. S
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Can anyone say if there is a good way to remember the precedence of operators?

Regards,
Jothi Shankar Kumar. S
 
reply
    Bookmark Topic Watch Topic
  • New Topic