Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Operator precedence and arrays

Abimaran Kugathasan
Ranch Hand
Posts: 2066

Since arrays are allocated at runtime. Can somebody explain the reason for this result. Thanks in advanced...

Raju Champaklal
Ranch Hand
Posts: 521
first array[index]is calculated to array[0]
so it is like array[0]=index=3
indiex becomes 3 and then the s3 is assigned to array[0]

the rule for this is that all the operands are calculated before any arithmetic operation

and then normal right to left rule for assignemnt operators

Abimaran Kugathasan
Ranch Hand
Posts: 2066
Raju Said......
the rule for this is that all the operands are calculated before any arithmetic operatio
.

But, this is done in a same line!

Let's take another example,

.

What is the difference?

Raju Champaklal
Ranch Hand
Posts: 521
ya whats the problem in this? 5 is assigned to c and then to b and then to a...

Raju Champaklal
Ranch Hand
Posts: 521
a is 9...b is 9 and c are evaulted to be 9 before the operation...after the operans have been calculated...then the assingemnt is done....5 to c to b to a

Abimaran Kugathasan
Ranch Hand
Posts: 2066
It's correct!
But my problem is with that array? Why can do like this to that array?

Abimaran Kugathasan
Ranch Hand
Posts: 2066
heee, Raju. you are wrong. a and b are not initialized on that line to 9. But that is not my problem. Mine is related to array on that code!

Raju Champaklal
Ranch Hand
Posts: 521
what do you mean....array is an operand there.....the value of index is 3....so array[index] is evaluated to array[0]....what didnt you understand here?

Neha Daga
Ranch Hand
Posts: 504
whenever there is assignment like this you can see it like as follows:
array[index] = (index = 3)
so when this line is executed array[index] will be evaluated with original value of index then right side(index = 3) will be evaluated and the expression will become:
array[0] = 3.
hope you understand or you need more detailed explaination?

Abimaran Kugathasan
Ranch Hand
Posts: 2066
If you can, Please give some more. (Sir, I want some more - Oliver Twist). Thanks in Advanced.

Nidhi Sar
Ranch Hand
Posts: 252
Abimaran Kugathasan wrote:Raju Said......
the rule for this is that all the operands are calculated before any arithmetic operatio
.

But, this is done in a same line!

I think Raju is right, it doesn't matter if they are on the same line, assignment operator is the last to act.

Everything else happens before the assignment happens, so array[index] is calculated before the assignment starts from right to left (as noted above).

Abimaran Kugathasan
Ranch Hand
Posts: 2066
Thanks a lot to all. I got it. Thanks Raju, Neha, Nidhi Sar.

Henry Wong
author
Marshal
Posts: 21506
84
Abimaran Kugathasan wrote:

Yes. it is related to operator precedence.... but that is not the only thing that has an effect here. You also need to understand the associativity and the order of evaluation for this.

For the precedence, the array dereference is higher than the assignment. Not that we have a choice here, if the assignment had higher precedence, the expression will generate a syntax error here.

For the associativity, it only applies to the assignments for this expression. And in this case, it just means that the second assignment has higher precedence than the first assignment.

And for the Order of Evaluation, that is defined by the specification, this is probably the main cause that effects what you are seeing. For the most part, Evaluation Order goes left to right, with side effects being applied as the expression is evaluated.

Henry

Abimaran Kugathasan
Ranch Hand
Posts: 2066
Great! Thanks Henry Wong!

Bert Bates
author
Sheriff
Posts: 8900
5
interesting discussion, but this sort of thing isn't on the exam.

Tapio Niemela
Ranch Hand
Posts: 77
Bert Bates wrote:interesting discussion, but this sort of thing isn't on the exam.

Indeed, but Bert, is the information in your (and Kathy Sierras) book (SCJP 6) enough to pass the exam. I mean how "deep" and tricky questions are there? You mentioned that this array/index-thing isn't on the exam. But how about, lets say, overloading with widening and var-args. I understand this pretty clearly, as it is stated in the book. But then again, it can get very complicated..

In this case we got compiler error, call to callMe is ambiguous. I've played with this thing quite a lot and I think I've learned things that aren't really clearly mentioned in the book, as those are very detailed and obscure cases..

It's probably nice to know these obscure cases like the back of your hand. That way one can predict which piece of code can cause errors/mysterious behaviour, and more easily see bugs in the code. However, is this "extra" information needed to pass the exam? Is the book enough?

thanks
Tapio

Nidhi Sar
Ranch Hand
Posts: 252
Henry Wong wrote:For the most part, Evaluation Order goes left to right, with side effects being applied as the expression is evaluated.

Hi Henry,

I know the evaluation order is normally left-to-right, but when it comes to assignment operator isn't the evaluation order right-to-left?

If line 1 were being evluated left-to-right (first set a to b, then b to c, then c to 99) wouldn't the result be different?

Raju Champaklal
Ranch Hand
Posts: 521
what Henry meant is that in some cases the order is from right to left like the one you gave above Nidhi

Abimaran Kugathasan
Ranch Hand
Posts: 2066
Hi, Nidhi Sar, That's not related to order of execution. That is related to associativity in Java. When two operators with the same precedence the expression is evaluated according to its associativity.
For Ex, x = y = z = 17; is treated as x = (y = (z = 17)), since = operator has right to left associativity.
And,
Order of Evaluation : In Java, left operand is always evaluated before the right operand. It also applies to function arguments.

For Ex,

Henry Wong
author
Marshal
Posts: 21506
84
Raju Champaklal wrote:what Henry meant is that in some cases the order is from right to left like the one you gave above Nidhi

No... Order of evaluation is determined by the specification, and it is always left to right.

Nidhi Sar wrote:
I know the evaluation order is normally left-to-right, but when it comes to assignment operator isn't the evaluation order right-to-left?

If line 1 were being evluated left-to-right (first set a to b, then b to c, then c to 99) wouldn't the result be different?

Precedence and association is applied before evaluation. So, once you apply the precedence and assoc, you get...

At this point, the expression is evaluated... and the order of evaluation on this expression, is left to right.

In Summary, do not confuse precedence, and order of evaluation. Association is applied to further define precedence. It is not used for the order of evaluation.

Henry

Nidhi Sar
Ranch Hand
Posts: 252
Thanks Abimaran, Henry. That makes it very clear.

- Nidhi

Abimaran Kugathasan
Ranch Hand
Posts: 2066
Nidhi Sar wrote:Thanks Abimaran, Henry. That makes it very clear.

- Nidhi

You are Welcome!