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
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Problem

Ranch Hand
Posts: 38
• Number of slices to send:
Optional 'thank-you' note:
hi friends ,
i have some doubts about associativity and precedence
can anybody clear my concepts ?
please if possible explain me with examples
rahul

"The Hood"
Posts: 8521
• Number of slices to send:
Optional 'thank-you' note:

Rahul Pawar
Ranch Hand
Posts: 38
• Number of slices to send:
Optional 'thank-you' note:
i don't know how associativity and precedence work .
rahul

Ranch Hand
Posts: 3141
• Number of slices to send:
Optional 'thank-you' note:
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.