• 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 ? from JQ+

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
can any one explain me why the output for the following code is 1.
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 ] ] );
}
}
Thanks
Ketu
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting..! Let me try.
The expression (a = b), assigns b to a and returns a reference to an int array. Let's call it x. Then you can write the whole
expression as a[x[3]]. x[3] is 0, since it is the fourth element in the array b. So the expression becomes a[0], which is 1 and that's what being printed. But, if you try to print a[0] after the println statement, it will print 2, because the array that
was referenced by a before the assignment is no longer accessible - a and b both point to the same array which b was referencing.
HTH
------------------
Velmurugan Periasamy
Sun Certified Java Programmer
----------------------
Study notes for Sun Java Certification
http://www.geocities.com/velmurugan_p/
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This one is a little tricky. But what happens is the following:
System.out.println( a [ ( a = b ) [ 3 ] ] );
1. The first statement to be evaluated is a[...] which says you want to collect an element from the array which is referenced from a.
2. Secondly (a = b) will be evaluated. So from now on the reference of a will point towards the same array as b.
3. Which element is it that we want to collect? The 4th one from the b array (since array indexes start at 0), which is the integer 0.
4. An last but not least the final value to be printed out will be the 0 element of a[...] which still points towards the old reference {1,2,3,4}.
I must admit that I am not 100% sure about my theory, but experementing with this piece of code brought me to this conclusion.
Regards
Dominic Steng�rd
Sun Certified Programmer for the Java 2 Platform
[This message has been edited by Dominic Steng�rd (edited February 05, 2001).]
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A confusing thing about Arrays, is that Java will always try to find the array element before altering the Array. You would think that a would reference the array at b, but I don't beleive it will until the next line of code.
- David
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic