• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Operator precedence and arrays

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Since arrays are allocated at runtime. Can somebody explain the reason for this result. Thanks in advanced...
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's correct!
But my problem is with that array? Why can do like this to that array?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you can, Please give some more. (Sir, I want some more - Oliver Twist). Thanks in Advanced.
 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot to all. I got it. Thanks Raju, Neha, Nidhi Sar.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great! Thanks Henry Wong!
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interesting discussion, but this sort of thing isn't on the exam.
 
Ranch Hand
Posts: 90
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Abimaran, Henry. That makes it very clear.

- Nidhi
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

- Nidhi



You are Welcome!
 
reply
    Bookmark Topic Watch Topic
  • New Topic