• 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

What would be the Rule for Multiple One-Line assignments?

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdi fellas!

Guess what?, I've been taking some Mock-Tests around, & There's this neat site called: JavaBlackBelt which has some free Mocks and a nice online Testing Engine, although I passed all the tests; Here's an Excerpt from Sahir Shah's Test back there in JavaBlackBelt which is really confusing (To me):



It's really twisted code I'd say, because the explanation states that the assignments are evaluated from left to right.

If that's so, How come the literal value 2 Gets assigned to Array a in position 0?[/B]

According to my logic only var b should be assigned with the value 2
But, as it turns out Array a at index 0 is assigned the value 2 too.

I'm really confused,
What's the rule here?

Thanks in advance for all the help as always,
Jose

It would seem the line I'm interested in for you to analyze, does not appear correctly: Let me put it again just in case:

a[a[b]] = a[b] = b = 2; //How does this line work?

[ January 09, 2008: Message edited by: Jose Campana ]
[ January 09, 2008: Message edited by: Jose Campana ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An assignment operator is "syntactically right associative"

That is, a = b = c means a = (b = c)

What we have here is an array access expression.

By array access expression , i mean something like a [ i ]

a is referred to as array reference subexpression

i is referred to as index subexpression.

JLS specifies that when an array access expression is used with a simple assignment operator ( =),
here are the simplified steps:
1. the array reference subexpression is evaluated.
2. the index subexpression is evaluated.
3. the right hand operand is evaluated.

Let us consider this now:

a[a[b]] = a[b] = b = 2;

Surrounding it with braces,

a[a[b]] = (a[b] = (b = 2));

This is in form:

Operand Left = Operand Right

1. the array reference subexpression is evaluated.

Nothing to be evaluated

2. the index sub expression is evaluated

3. right operand evaluated..

Steps 2 and 3 will have to be done "recursively" (if that is the right word?)
a[a[b]] = (a[b] = (b = 2));

becomes (since b is 0)

a[a[0]] = (a[b] = (b =2));

becomes

a[0] = (a[b] = (b =2));

becomes

a[0] = (a[b] = (b=2));

becomes

a[0] = (a[0] = (b=2));

b is assigned 2

becomes

a[0] = (a[0] = 2);

a[0] is assigned 2

becomes

a[0] = 2;

a[0] is assigned 2 again

Thus, a[0] is 2 and b is 2 finally...

Please refer to JLS 15.26.1 third edition pg 513

[ January 09, 2008: Message edited by: Raman Gopalan ]

[ January 09, 2008: Message edited by: Raman Gopalan ]

[ January 09, 2008: Message edited by: Raman Gopalan ]
[ January 09, 2008: Message edited by: Raman Gopalan ]
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Raman,

Very nice explanation!!
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Raman, very nice explanation indeed, however I feel like I still need to understand it more throughly, perhaps some exercises will help memorize these rules.

Thank you very much,
If anybody has any further comments, please feel free to post.

Best Regards,
Jose
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic