jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operator Associativity 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 "Operator Associativity" Watch "Operator Associativity" New topic

Operator Associativity

Saniya Ansari
Ranch Hand

Joined: Sep 30, 2002
Posts: 48
Hi Dan (specially coz i'm doign ur mock exams), I tried this question in Exam D:
class L {
public static void main (String s[]) {
int i = 1 | 2 ^ 3 * 2 & 13 | 2;
The answer is 2. Your explanation is also helpful.. i'm wondering are there any rules to follow when we evaluate such an expression. It is left associative but how shoudl we group them.. i.e how do we get (1 | (2 ^ ((3 * 2) & 13)) | 2)
instead of some other arrangement.
So someone can help?!

Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Please see the following tutorial and operator precedence chart.
I hope that helps.

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Wena Dollison

Joined: Jul 03, 2002
Posts: 7
Just remember these abbreviation:
U - Unary
A - Arithmetic
S - Shift
C - Comparison
B - Bitwise
S - Short-Circuit
C - Conditional
A - Assignment
So unary operators come first, then arithmetic operators, and so on. * always come first in the arithmetic operators (if not grouped or within parentheses or associated) and & then ^ then | is the order for the Bitwise operators
You can also read this in RHE Java Certification book
I agree. Here's the link: http://aspose.com/file-tools
subject: Operator Associativity
It's not a secret anymore!