This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operator precedence and arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Operator precedence and arrays" Watch "Operator precedence and arrays" New topic
Author

Operator precedence and arrays

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066



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


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
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


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
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

Joined: Dec 10, 2009
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

Joined: Dec 10, 2009
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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Nov 04, 2009
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

Joined: Dec 10, 2009
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

Joined: Oct 30, 2009
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?


SCJP 1.6 96%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

If you can, Please give some more. (Sir, I want some more - Oliver Twist). Thanks in Advanced.
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
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).

"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks a lot to all. I got it. Thanks Raju, Neha, Nidhi Sar.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Great! Thanks Henry Wong!
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8805
    
    5
interesting discussion, but this sort of thing isn't on the exam.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Tapio Niemela
Ranch Hand

Joined: Jan 06, 2006
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

Joined: Oct 19, 2009
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?

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?
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
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

Joined: Nov 04, 2009
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,

Please correct, If I'm wrong!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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.

Henry
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Thanks Abimaran, Henry. That makes it very clear.

- Nidhi
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Nidhi Sar wrote:Thanks Abimaran, Henry. That makes it very clear.

- Nidhi


You are Welcome!
 
 
subject: Operator precedence and arrays
 
Similar Threads
ne 1 from b'bay giving scjp in feb
Doubt on a sample question from sun
associativity and precedence...prob..pls..help
need help on precedence of operators
Assignment