While learning how to declare, initialize, and access array elements in java, I wrote this simple code:
class ArrayAccess{ public static void main(String args[]){ int[] a; a = new int[4]; a[0] = 23; a[1] = a[0]*2; a[2] = a[0]++; a[3]++; int i = 0; while(i < a.length){ System.out.println(a[i]); i++; } } }
But I get an unexpected result.
The output I get is:
24 46 23 1
So,
Why 24 instead of 23 as the value of a[0] ? If this corresponds to the increment a[0] in a[2] , then why is the element a[1] 46 , not 48 .
why 23 instead of 24 as the value of a[2] ?
source share