Skip to main content
edited tags
Link
Kevin Bowersox
  • 94.6k
  • 19
  • 164
  • 198

I am trying out the following Java snippet:

int[] testArray={10,20,30,40};  int i= 0;  testArray[i++]= testArray[i++]+1;  System.out.println("The value of i: "+i);  for(int i1=0;i1<testArray.length;i1++)  {   System.out.println(testArray[i1]);  }   } 

With i=0, the output values of the array are: 21, 20,30,40 21, 20,30,40

I can,tcan't understand this output because the output should be: 10, 11,30,40 testArray[0]+1 10, 11, 30, 40

testArray[0]+1 would be 1111 and it would be then assigned to testArray[1]testArray[1] but this is not the case. Can anybody explain the output?

I am trying out the following Java snippet:

int[] testArray={10,20,30,40};  int i= 0;  testArray[i++]= testArray[i++]+1;  System.out.println("The value of i: "+i);  for(int i1=0;i1<testArray.length;i1++)  {   System.out.println(testArray[i1]);  }   } 

With i=0, the output values of the array are: 21, 20,30,40

I can,t understand this output because the output should be: 10, 11,30,40 testArray[0]+1 would be 11 and it would be then assigned to testArray[1] but this is not the case. Can anybody explain the output?

I am trying out the following Java snippet:

int[] testArray={10,20,30,40}; int i= 0; testArray[i++]= testArray[i++]+1; System.out.println("The value of i: "+i); for(int i1=0;i1<testArray.length;i1++) { System.out.println(testArray[i1]); } 

With i=0, the output values of the array are: 21, 20,30,40

I can't understand this output because the output should be: 10, 11, 30, 40

testArray[0]+1 would be 11 and it would be then assigned to testArray[1] but this is not the case. Can anybody explain the output?

Source Link
user1107888
  • 1.5k
  • 5
  • 35
  • 58

Pre/Post increment operators and arrays

I am trying out the following Java snippet:

int[] testArray={10,20,30,40}; int i= 0; testArray[i++]= testArray[i++]+1; System.out.println("The value of i: "+i); for(int i1=0;i1<testArray.length;i1++) { System.out.println(testArray[i1]); } } 

With i=0, the output values of the array are: 21, 20,30,40

I can,t understand this output because the output should be: 10, 11,30,40 testArray[0]+1 would be 11 and it would be then assigned to testArray[1] but this is not the case. Can anybody explain the output?