# Array question

Wu Wen
Greenhorn
Posts: 27
I'm completely lost, I don't know why it prints 1.

Stephan van Hulst
Bartender
Posts: 5885
63
Why? What do you expect it to print?

Matthew Brown
Bartender
Posts: 4567
8
• 1
There are two key points to this:

- The assignment operation a = b assigns b to a and returns b.
- Expressions are evaluated left-to-right

Does that help it make sense? If not, what are you expecting to happen?

Astha Sharma
Ranch Hand
Posts: 250

code assigns reference of b to a. Thus both a and b starts referring to array object {2,3,1,0}. Another object {1,2,3,4} losses its reference. Now expression becomes a[a[3]]. Value at a[3] is 0. Expression becomes a[0]. Value at a[0] is 2 since a now refer to array object {2,3,1,0}. Then why it prints 1?

Carlos Renato Domingos da Silva
Greenhorn
Posts: 4
• 1
Hi the object array a, not lost the reference. It is working with dynamic values. When the code get a=b between parentesis not mean the lots the reference.

step1 a = b so, a = 2, 3, 1, 0 (but not change reference a because is a dynamic call.
step2 give a[3] = 0
step3 give a[a[3]] on originally a (before 1, 2, 3, 4).

so, prints 1. If you have a doubts, make a debug on eclipe or netbeans.

best regards;

Henry Wong
author
Marshal
Posts: 21184
80
• 1
Astha Sharma wrote:
code assigns reference of b to a. Thus both a and b starts referring to array object {2,3,1,0}. Another object {1,2,3,4} losses its reference. Now expression becomes ....

No. There is no "now expression becomes". It is a single expression. And the order of evaluation of the expression is from left to right.

This means, at the point of the assignment, at the point that you are referring to, the effect of the changes are only to the evaluations to the right of that point. The dereferencing to the array object, to the left of that point, has already been done, and goes to the original array that a was referencing.

Henry

Wu Wen
Greenhorn
Posts: 27
Thank you, I understand now.

Henry Wong wrote:
Astha Sharma wrote:
code assigns reference of b to a. Thus both a and b starts referring to array object {2,3,1,0}. Another object {1,2,3,4} losses its reference. Now expression becomes ....

No. There is no "now expression becomes". It is a single expression. And the order of evaluation of the expression is from left to right.

This means, at the point of the assignment, at the point that you are referring to, the effect of the changes are only to the evaluations to the right of that point. The dereferencing to the array object, to the left of that point, has already been done, and goes to the original array that a was referencing.

Henry

Astha Sharma
Ranch Hand
Posts: 250
got it........thanks

O. Ziggy
Ranch Hand
Posts: 430
I thought that brackets would always be evaluated first. And also that the left to right rule will only be used if the operators have the same precedence. This seems to go against these rules..

Matthew Brown
Bartender
Posts: 4567
8
It's slightly more subtle than that. The left-to-right rule means that the first thing that's done is deciding what the first a is referring to. It's referring to the array { 1, 2, 3, 4 }. This will not change, even when a gets reassigned when the rest of the expression is evaluated. In effect the reference of a has been copied for use in the first part of the expression.

The best approach (when not answering certification questions) is to avoid this sort of confusion completely - as you've seen the results can be counter-intuitive. So avoid using expressions where part of it has a side effect - like assigning b to a in this case - unless you're sure it's safe. If you avoid side effects, then it will all work as you currently understand it.

Henry Wong
author
Marshal
Posts: 21184
80
O. Ziggy wrote:I thought that brackets would always be evaluated first. And also that the left to right rule will only be used if the operators have the same precedence. This seems to go against these rules..

What you are describing are the precedence and the associativity. What we are talking about is order of evaluation. These three things are *not* the same things. All three of these rules apply together.

For example....

Precedence says that multiply is higher, hence ...

Associativity says that addition and multiply are both left to right, hence ...

Now... as for evaluation, remember that a computer is *not* human. Most people will resolve this expression from the inside out -- as that is the easiest way to do it. And if you are good, it can even be done without any paper.

Java follows the rules in the JLS for evaluations, and for the most part, it is done from left to right. This means, as a possible implementation detail, lots of temporary variables (or likely a stack), to remember stuff, as the evaluation is moving left to right.

Henry

Astha Sharma
Ranch Hand
Posts: 250
Got more confused now. Would anyone please explain step by step evaluation of this expression...

Matthew Brown
Bartender
Posts: 4567
8
It's effectively doing this:
If you follow that through, does that help?

Stephan van Hulst
Bartender
Posts: 5885
63
• 2
Or you can evaluate the statement step by step, from left to right:

Astha Sharma
Ranch Hand
Posts: 250
yes...it helped a lot. Now its clear to me. Thanks to all for the explanation.