Precedence of operators in Java -
this question has answer here:
- the difference between ++var , var++ 7 answers
when run java code:
int[] = new int[10]; int = 0,j = 0; while(i < 10){ a[i++] = j+++j++; } system.out.println(arrays.tostring(a));
i output: [1, 5, 9, 13, 17, 21, 25, 29, 33, 37]
. can please explain how statement a[i++] = j+++j++
resolved.
the first j++
in expression j+++j++
increments j
, returns previous value.
the second j++
increments j
, returns previous value, value after first j++
incremented it.
at start of next iteration, j
larger 2 compared value @ start of previous iteration (since previous iteration incremented j
twice).
therefore :
a[0] = 0++ + 1++ = 0 + 1 = 1; a[1] = 2++ + 3++ = 2 + 3 = 5; a[2] = 4++ + 5++ = 4 + 5 = 9; ...
Comments
Post a Comment