Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# operators

nitin sharma
Ranch Hand
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
Posts: 290
some body please come up with something.

Cindy Glass
"The Hood"
Sheriff
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.

Indika Perera
Greenhorn
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