• 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: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A
{
public static void main(String[] args)
{
int[] a = new int[10];
int b=0;
int x;
a[a[b]]=a[b]=b=2;
System.out.println(a[x]);
//What should be the value of x so that it returns non zero value
}
}

//why the value of x comes out to be 0
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First note that when the int array is created, each element is initialized to zero.

Basically, in a simple assignment, the left-hand operand is evaluated first to figure out exactly where the value of the right-hand expression should be assigned -- for example, a variable or a particular index of an array. In this case, we have...

int b = 0;
a[a[b]] = ...

Here, a[b] is a[0], which is 0. So a[a[b]] is also a[0].

And the line a[a[b]] = a[b] = b = 2 essentially becomes a[0] = (a[0] = (b=2)). So the value 2 is assigned to the variable b, which is assigned to a[0], which is assigned to a[0].

Therefore, a[0] is 2, while a[1] through a[9] are all zeros.
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lalit,

int[] a = new int[10];
int b=0;
int x;
a[a[b]]=a[b]=b=2;
System.out.println(a[x]);

In the above statement first all the variables are fully evaluated and then the assignment happens that is the statement becomes:

a[0]=a[0]=b=2

Since before this statement b=0 and a[b]=0 and as the question ask for a value x of that will give non-zero value, result becomes 0 b'coz that is the only index position where we've stored 2.

Hope this helps
Asha
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Lalit,

class A
{
public static void main(String[] args)
{
int[] a = new int[10];
int b=0;
int x;
a[a[b]]=a[b]=b=2;
System.out.println(a[x]);
//What should be the value of x so that it returns non zero value
}
}

in the above program, the variable i is not initialized. and since its a local variable, it does not get the default value. so this above coding results in a compilation error.
variable x might not have been initialized
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vidhyakar vidhyakar:
...this above coding results in a compilation error.
variable x might not have been initialized


Right, but the question is: "What should be the value of x ... ?"
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"vidhyakar vidhyakar,"

You've been asked once before to revise your display name to meet the JavaRanch Naming Policy. You can edit your name here. Thank you for your prompt attention!

-Marc
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic