This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem" Watch "Problem" New topic


Rahul Pawar
Ranch Hand

Joined: Mar 13, 2001
Posts: 38
hi friends ,
i have some doubts about associativity and precedence
can anybody clear my concepts ?
thanks in advance.
please if possible explain me with examples
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
What are your doubts?

"JavaRanch, where the deer and the Certified play" - David O'Meara
Rahul Pawar
Ranch Hand

Joined: Mar 13, 2001
Posts: 38
i don't know how associativity and precedence work .
please help me out ....
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Rahul,
'precedence' refers to the priorities given to the various operators. When a Java expression is evaluated operator precedence rules are applied; similar to the way Math expressions are evaluated. For example, parantheses are evaluated first, then mulitiplication/division, then addition, subtraction, etc.
For example, in the expression <code>5 + 3 * 2</code>, mulitiplication has higher precedence than addition, so the expression is evaluated as <code>5 + 6</code> with a result of 11.
If parantheses are used <code>(5 + 3) * 2</code>, the portion within the parantheses is evaluated first <code> 8 * 2</code> with a result of 16.
'associativity' refers to the order in which operators are evaluated when operators have the same level of precedence. In Java, expressions are evaluated left-to-right, except for assignments, which are evaluated right-to-left.
For example, <code>5 - 3 + 2</code> is evaluated left-to-right <code> 2 + 2 </code> with a result of 4.
Assignment is right-to-left, <code> a = b = c = 5</code> first 5 is assigned to 'c', then the value of 'c' is assigned to 'b' and then the value of 'b' is assigned to 'a', with the result, a = 5.
For more information on how Java evaluates expressions read JSL §15.7
Hope that helps.

Jane Griscti
Sun Certified Programmer for the Java� 2 Platform

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
I agree. Here's the link:
subject: Problem
It's not a secret anymore!