General Comments: In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated.
So, in the expression a[(a=b)], the expression a is fully evaluated before the expression (a=b); this means that the original value of a is fetched and remembered while the expression (a=b) is evaluated. This array referenced by the original value of a is then subscripted by a value that is element 3 of another array (possibly the same array) that was referenced by b and is now also referenced by a. So, it's actually a = 1.
Note that, if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.
Imagine how would it be if the array object that was initially referred by the reference a is GCed after a=b?
Write your code as if the person who maintains it is a homicidal maniac who knows where you live.
Hrishikesh Yeshwant Alshi wrote:
If so, could you please elaborate a bit? How can same reference 'a' refer to two different array objects?
Basically, it can't. One reference can't refer to two array objects at the same time. Yet, in order for java to process the expression, it seems to need for it to do so. So, how is it done? Simply, with temporary variables. While the JVM is processing the expression, it has temporary variables -- which hold interim values while the processing is happening.
And these temporary variables are uses, just like any other variables, for the GC to determine reachability. In other words, the GC can't collect the object originally referenced by "a" because there is a temporary variable that reaches it. This temporary variable will, of course, go out of scope once the expression is complete.