This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Question.

 
Alan Blass
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! From Enthuware:



Why is the result 1? It explains: "In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated.

In the expression a[(a=b)[3]], the expression a is fully evaluated before the expression (a=b)[3]; this means that the original value of a is fetched and remembered while the expression (a=b)[3] is evaluated.

This array referenced by the original value of a is then subscripted by a value that is element 3 of another array (possibly the same array) that was referenced by b and is now also referenced by a. So, it is actually a[0] = 1.

Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated."

I don't understand why. How is the a[(a=b)[3]], the expression a is fully evaluated before the expression (a=b)[3] ??

Can someone please explain? Thanks.
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.13.1

First, the array reference expression is evaluated. If this evaluation completes abruptly, then the array access completes abruptly for the same reason and the index expression is not evaluated.

Otherwise, the index expression is evaluated. If this evaluation completes abruptly, then the array access completes abruptly for the same reason.

Otherwise, if the value of the array reference expression is null, then a NullPointerException is thrown.

Otherwise, the value of the array reference expression indeed refers to an array. If the value of the index expression is less than zero, or greater than or equal to the array's length, then an ArrayIndexOutOfBoundsException is thrown.

Otherwise, the result of the array access is the variable of type T, within the array, selected by the value of the index expression.


first (step first) array reference expression is evaluated at this poin a - remembering. after index expression is eluvated (a=b)[3] - after this expression you will get index =0
after this value of the array (step first) check for null, after checking of valid index for array (step first)
and after that you will have a(firs ref) [0] and can get your element.
and after all a==b be true.
 
Alan Blass
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Does that mean b[3] is evaluated first to give 0, then a[0] gives 1?

What about (a = b)?
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
before you get index you must eluate
(a = b)[3]
for this you will eluate expression a=b, after this point a and b link to same array (created in this line int[] b = { 2, 3, 1, 0 }; )
but
a from a[(a=b)[3]] expression was memorized and link to expression created in this line (int[] a = { 1, 2, 3, 4 }; )
 
Alan Blass
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. I see it.

Working from left to right, A's array is first {1, 2, 3, 4}.

Then b is assigned to a, where A array becomes {2,3,1,0} .

Then a[3] is evaluated which is 0.

So taking a[0] of the first mentioned of A is 1.

Correct me if I am wrong.

Thanks a lot.
 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you right. and you will get same result if run code. also you can read this topic
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder if such syntax is on the exam I took 3 SCJP exams. Although many questions on arrays, I never encountered an array question with such a (weird) syntax. (And in my personal opinion, such syntax should not be on the exam)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic