The given problem is a good one on operator precedence .
int i = 5 at first
the expression assigns value to array[5] by :
array[i++]=i++ + i++;
array[i++] indicates array[5] after which i gets incremented by 1 .
rhs of exp encounters
i++ + i++
that is 6 ( i again gets incremented by 1 to 7) + 7 ( i again gets incremented by 1 to 8)
i.e array[5] = 6 + 7 = 13
System.out.println(array[5]+" "+array[6]+" "+i);
this prints out array[5] array[6](which is zero - default value) i ( which is 13)
hence the output should be 13 0 8
Originally posted by Meena:
Q2>
public class lost {
public static void main (String [] args){
int [] array = new int[10];
int i=5;
array[i++]=i+++i++;
System.out.println(array[5]+" "+array[6]+" "+i);
}
}
When i tried to execute this code,i got output as 13 0 8.
Can anybody try to explain this.