0.0 0.0 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at Foo.main(Foo.java:10) Press any key to continue . . . please explain me why it is
"j" is incremented twice in each loop iteration. You might want to print out the value of j before and after the "x[j]=j++" statement to see how its value changes.
x[j]=j++ assigns the current value of j to x[j], then increments it. So, if the other increment would be absent, it would result in an array of [0, 1, 2, 3, 4]. Also, after this increment, you're trying to access x[j] again.
So what is happening: x[0] = 0; j = 1; x[1] is printed (0 because not initialized) j = 2 (loop increment) x[2] = 2; j = 3; x[3] is printed (0 not initialized) j = 4 (loop increment) x[4] = 4; j = 6; x[5] is printed -> ArrayIndexOutOfBoundsException