File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# operators

nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
public class Q6
{
public static void main(String[] args)
{
int a[]={4,5,9,8,6,6,0};
int i=1;
a[i]=i+=a[i++];
System.out.println(a[i]+" , "+i);
}
}
please explain the way output is coming?
0,6
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
some body please come up with something.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
i starts at 1
so a[1] gives you 5 (the second element of the array is 5)
i+=5 gives you 6
a[6] gives you 0 (the 7th element of the array is 0).
So the value of i ends up 6, and the valus of a[i] is 0.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Indika Perera
Greenhorn

Joined: Feb 28, 2001
Posts: 8
hi, i got it this way. Hope it's correct.
a[i]=i+=a[i++];
initially i = 1 so, expression become, a[1] = i+ = a[1]
Now, Evaluating from Right to left
first i+=a[1]; i = i + a[1] = 6
then a[1] = 6 Therefore a[6] remains 0 and i is now 6

I agree. Here's the link: http://aspose.com/file-tools

subject: operators