File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Summarization required Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Summarization required" Watch "Summarization required" New topic
Author

Summarization required

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9353
    
    2

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 ]

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!
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
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 ]

SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9353
    
    2

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 Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9353
    
    2

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

Joined: May 02, 2006
Posts: 218
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 Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9353
    
    2

Hi Above,

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

Regards,
Jothi Shankar Kumar. S
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9353
    
    2

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 Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9353
    
    2

Hi All,

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

Regards,
Jothi Shankar Kumar. S
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Summarization required