• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

array doubt: a [ (a = b)[3] ]

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


Please help me in understanding Line #1, I am not getting at all.


Source:
Enthuware

Thanks,
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which version of enthuware are you using?
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its printing 1 i dont know why?
 
sharan vasandani
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think when the compiler encounters
int[] b = { 2, 3, 1, 0 };
a[3] after solving inner part.


System.out.println( a [ (a = b)[3] ] );

due to first a which i have made bold at this point a is referring to this object { 1, 2, 3, 4 } then it solves the inner part due to which now
a[3] becomes 0,so the compiler sees 1 at index position 0 of the first array.

am not sure about this.
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi chandra,
Reference of b is passed to A. So, now a is refering to b. This was my observation.

int[] a = {1,2,3,4};
int[] b = {2,3,1,0};


System.out.println(a[ (a = b) [3] ] ); // prints 1
System.out.println(a[ (a = b) [2] ] ); //prints 2
System.out.println(a[ (a = b) [1] ] ); // prints 4
System.out.println(a[ (a = b) [0] ] ); //prints 3

Here the last element is printed as 3rd element and 3rd element is printed as last element but when you print "A" elements. It prints 2,3,1,0
because it is refering to b.Can anybody tell when the "B" reference is passed to "A" why is it printing like that?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can anybody tell when the "B" reference is passed to "A" why is it printing like that?


(a=b) here the b address is being assigned to a.
so a is refering to b now.
meaning you can read like this a[ (a = b) [1] ] like a[ b [1] ]
now.
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wong,
Look at the statement below
Hi wong,

int[] a ={1,2,3,4};
int[] b={2,3,1,0};
System.out.println(a[ (a=b)[3] ]);

In this statement (a[ (a=b)[3] ]) when will the cast happen because output is 1. So as you explained the above statement will be a[b[3]] since b[3] returns "0" then the statement will be a[0] now since "A" is refering to "B" output should be 2 but why it is prinring 1.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More info on same question.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int[] a ={1,2,3,4};
int[] b={2,3,1,0};

System.out.println(a[ (a=b)[3] ]);

a[(a=b)[3]] --> a[b[3]] --> a[0] --> 1

This is the first time I'm seeing such a question, very interesting!
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI the output of the code will be one

int[] a ={1,2,3,4};
int[] b={2,3,1,0};

System.out.println(a[ (a=b)[3] ]);

This exp will evaluate a=b first, so the reference of b will be passed on to a and b[3] that is 0 will inserted so the exp now becomes a[0] and the output will be 1.


I think this question was posted earlier too.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic