Mian Amjad wrote:... But element 3 in b[2,3,1,0] is 0. Why the out put is 1?
Element three is zero... okay. You got that... Then you are using zero to dereference the "a" array -- and the zeroth element is one.
There is also another point... the side effect of changing the "a" array reference to point to the same array as the "b" array reference doesn't happen until after the "a" reference is dereferenced, so, it is using the previous array (before the side effect).
That is different from what was originally written. Burkhard Hassel explains that the ==> token, which isn't a valid Java® operator, was simply used to show the current values of the different variables.
I hope that sort of code doesn't come up in the exam because it is horrible style and confusing, and if used at work will ensure that you are fired pretty quickly.
Evaluating that code requires two bits of information:-
2: As described in the same JLS section, precedences of operators and () are preserved.
So, ignoring the System.out.println part and only looking at the evaluation of the expression passed as its argument:-It would be easier to understand like this:-You can then see that the result will be 11 and there is no risk of confusing it with the 3rd element of the other array.
As stated previously the assignment in that horrible bit of code does not equate to a = b, not even if the “after” value of a is the same as the “before” value of b. That is why the output of my last line (5) changes from 11 to 2.