This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
Joined: Dec 10, 2009
Posts: 521
posted
0
ya whats the problem in this? 5 is assigned to c and then to b and then to a...
Raju Champaklal
Ranch Hand
Joined: Dec 10, 2009
Posts: 521
posted
0
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
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
Joined: Dec 10, 2009
Posts: 521
posted
0
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
Joined: Oct 30, 2009
Posts: 504
posted
0
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?
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.
interesting discussion, but this sort of thing isn't on the exam.
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
Tapio Niemela
Ranch Hand
Joined: Jan 06, 2006
Posts: 76
posted
0
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?
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.
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?
Please consider the following code:
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.