5

What is the logic behind this behaviour?

 int i=0;
    for(int k=0;k<10;k++){
    i++;
    }
    System.out.println("i="+i);

Output=10; //Exepcted



 int i=0;
    for(int k=0;k<10;k++){
    i=i++;
    }
    System.out.println("i="+i);

Output=0; //Surprised :) 

Can anybody throw some light on above functionality?

vivek shetty
  • 79
  • 1
  • 1
  • 4

8 Answers8

13

See this brilliant answer:

x = x++;

is equivalent to

int tmp = x;
x++;
x = tmp;

From this question.

Community
  • 1
  • 1
Maroun
  • 91,013
  • 29
  • 181
  • 233
  • 3
    anybody trying to understand `x=x++` keep in mind that post-increment has given priority over the assignment and x++ increments but returns old value. – chirag soni Jan 28 '20 at 16:27
7

i = i++ is a postfix increment operator - it increments i, then returns it to its original value (because the i++ essentially "returns" the value of i before it was incremented.)

i = ++i would work since it's a prefix increment operator, and would return the value of I after the increment. However, you probably just want to do i++ there without any extra assignment as you do in the first run - it's (essentially) shorthand as it is for i = i+1.

Michael Berry
  • 66,225
  • 20
  • 144
  • 202
5

What is happening happens because java is pass-by-value.

In the first loop, i is getting incremented in the i++ statement, however, in the second loop what is happening is that i gets pointed to a new memory location that stores the value of i (in this case 0) and then increments the old location.

To visualise:

i => 0x00000001 // 0

for() {
    i => 0x00000002 <- 0  // store old i value (0) in new location
    0x00000001++          // Increment the value stored at the old location

    // Cause there is no longer a reference to 0x00000001, 
    // it will get garbage collected and you will be left with
    // i => 0x00000002

And it will keep doing that, assigning the old value to a new location and incrementing the old value for each pass of the loop

TheMerovingian
  • 639
  • 4
  • 14
2

i=i++; will never increment i because the ++ is processed after the i=i.

you could see it like this:

int i=0;
for(int k=0;k<10;k++){
    int j = 0;
    i = j;
    j = j + 1;
}
Philipp Sander
  • 9,891
  • 5
  • 43
  • 78
2
i = i++;

is equivalent to,

int temp = i; // temp = 0
i++; // i=1
i = temp; // i = 0
AllTooSir
  • 47,910
  • 16
  • 124
  • 159
1

The ++ operator is processed after the assigment,

if you changed it i=++i; you'd probably get the behaviour you expected

paul
  • 21,148
  • 1
  • 51
  • 54
1

In the first option , you are incrementing the i by using i++ , which is equivalent to i=i+1, so it increases the value of i to 10.
but in the second option , you are assining i a new value , hence getting the same value everytime.

Vineet Singla
  • 1,539
  • 1
  • 18
  • 32
1

i=i++;

returns i and increments. so the increment is lost... look at this pseudo code

x = i++ will break the operation in following steps

x = i;
i++;

in your case , it returns 0 increments to 1 (but the increment is lost)

MemLeak
  • 4,228
  • 4
  • 41
  • 80