• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Question

 
Lalit mishra
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • 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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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.
 
Asha Pathik
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • 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
 
K Vidhyakar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic