• 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

Array Question.

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you right. and you will get same result if run code. also you can read this topic
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic