• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

verry confusing array !!!!

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..
Iam very confused with this array.
Plzz tell me ...how to evaluate the expression -
(a [ (a = b) [ 3 ]] )
class testarry
{
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]] );
}
}
The program prints 1. why???
Thanxx in advance.
S U N I T.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sunitkumar Survase:
(a [ (a = b) [ 3 ]] )
In this experssion you may notice there are 2 arrays(outer a[] array and an inner (a=b)[3]---> b[3] array) both arrays will be get evaluvated to
a[b[3]]
b[3] is 0.
So a[0]. which in turn will give 1
-Siva
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sunit,
It is all about operator precedence.
First a[i] is computed and printed, where 'i' is (a=b)[3].
To evaluate (a=b)[3], first assign array b into a, then take the subscript [3]. So a and b are denoting the same array { 2, 3, 1, 0 }. Thus i is equal to 0 (a[3] / b[3]). But as I mentioned before, a[i] is evaluated first. So a[0] from original array a ({ 1, 2, 3, 4 }) is printed. If you analyze array a after this 'println' statement, you will find that a referencing the array b ({ 2, 3, 1, 0 }).
Bala Arul.
 
Sunitkumar Survase
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanxx a lot.
I got it.
Cheers,
Sunit
 
That feels good. Thanks. Here's a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic