Hi Java ninjas!
I came across the below question while using the Enthuware test software. I'm sure it's actually quite simple to explain, however, Enthuware has a very long and complicated explanation that I do not understand. Why is the output 1 here?
Below is the explanation from the website:
n 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.
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 is 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.