• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

array

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Consider this code,
class Test {
public static void main(String[ ] args) {
int[ ] a = { 1, 2, 3, 4 };
int[ ] b = { 2, 3, 1, 0 };
System.out.println( a [ ( a = b ) [ 3 ] ] ); // o/p is 1
}
}
Can anyone explain the output
Thx
Aruna
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about the order of evaluation of the complex mess that is the index for a
( a = b ) result b
b[ 3 ] result 0
a[ 0 ] result 1
Bill
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK
a [ ( a = b ) [ 3 ] ] is a [x] , where
x = ( (a = b) [ 3 ] )
Let's print x:
System.out.println( (a = b) [ 3 ] ); // output is 0
Actually (a = b) [ 3 ] means a[3] except var a now points to array b, so whole expression means b[3], which is 0
And finally, a[x] = a[0] = 1, and I believe var a here still refers to array a, not b!
I wonder if somebody can translate all above to English�
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
An old thread was found when I do a search on "array".
I'm a bit confused here.. so, could anyone please help me figure this one out?
As Mapraputa Is described, a[x] is still pointing to the old reference of array a. Could anyone explain why??
I have tried the following code:

The current code will give an output of 1, because we still refer to the old a. But, if I run the "commented-out" code, then I'll get 2 as the output. Could anyone explain why?
And, if I still use the current code, then would it be in "lineOfGC", the a object is not eligible for GC??
Any help is highly appreciated..
Thanks in advance.
- eric
ps: Another thing to note is that when I try to print the elements of a, after the "lineOfGC", I get the elements of b.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the explanation that I could come up with:
"System.out.println( a [ ( a = b ) [ 3 ] ] ); "
results in a[b[0]] ... b/c evaluation is done left to right...
now in a[(a=b)[3]], the first ex. a does not need any further evaluation... next expression a=b results in a pointing to b's elements; 3 does not need any further eval... thus (a=b)[3] results in b[3] which is 0....adding it all it is a[0] which results in 1
Vaneet

Originally posted by Aru:
Hi all,
Consider this code,
class Test {
public static void main(String[ ] args) {
int[ ] a = { 1, 2, 3, 4 };
int[ ] b = { 2, 3, 1, 0 };
System.out.println( a [ ( a = b ) [ 3 ] ] ); // o/p is 1
}
}
Can anyone explain the output
Thx
Aruna


 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aru,
Please change your name to be compliant with JavaRanch's naming policy.
Your ID should be 2 separate names with more than 1 letter each. We really want this to be a professional forum and would prefer that you use your REAL name.

------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Just to clear Bill's short explanation (which is right on!). Eric's question is a valid one. The problem that everyone seems to be missing is that the assignment operator is the last one to complete in the line. That is why the print will still be referring to the original a array because the assignment happens afterwards (lowest eval priority). Also, Eric correctly points out that after the print line is complete variable a is pointing the same array as variable b.
Bill's first line says (a = b) result = b, what is not said is that the assignment has not happened yet, we are just using the result to complete the print! The assignment will happen after the value a[0] has been used for the print.
Regards,
Manfred.
 
Eric Pramono
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, Manfred.
That really helps.
- eric
 
If we don't do the shopping, we won't have anything for dinner. And I've invited this tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic