• 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
  • 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

Confusion with operator '=' right associativity

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Please check the following code



When i have seen this i thought as the associativith is right the code will execute as following

a[i] = i = 9;
here right to left so i will be 9;
a[9] = 9;

but the program is running as a[0] = 9; Why is this?Could some body explain this.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Operands are evaluated from left to right, this rule takes precedence over any other rules that govern expressions.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct for assignments, but you need to account for the fact that array reference subexpressions are evaluated first, before the assignments take place. So the assignment steps becomes:

then

leading to
 
Craig Bayley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Ahmed, but assignment operations are right associative - the gotcha is the array subexpression reference is evaluated first. The poster is correct in believing assignments are evaluated from the right.
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assignment operators are Right associative which means:


a = b += c = 5;
is translated to:
a = (b += (c = 5))


But that said nothing about Operands evaluation which is always left to right 'a' 'b' 'c' are evaluated in this order.
And its true the dimension expression for arrays is evaluated first, but its irrelevant to this topic.
Read JLS sections 15.7.1 and 15.7.2
 
Craig Bayley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Ahmed, but I misinterpreted what you meant. I think it would have been clearer if you'd said that the java language requires all operands to be fully evaluated before operations take place, which means that the array subexpression has already been evaluated before the assignments were evaluated. I appreciate my statement should have been clearer as to this as well.
 
vijaya bharath
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much
 
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic