File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operators" Watch "operators" New topic


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;
System.out.println(a[i]+" , "+i);
please explain the way output is coming?
nitin sharma
Ranch Hand

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

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

Joined: Feb 28, 2001
Posts: 8
hi, i got it this way. Hope it's correct.
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:
subject: operators
It's not a secret anymore!