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

14

See this brilliant answer:

x = x++;

is equivalent to

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

From this question.

Community
  • 1
  • 1
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • 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
  • 70,193
  • 21
  • 157
  • 216
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
  • 648
  • 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
  • 10,139
  • 6
  • 45
  • 78
2
i = i++;

is equivalent to,

int temp = i; // temp = 0
i++; // i=1
i = temp; // i = 0
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
1

The ++ operator is processed after the assigment,

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

paul
  • 21,653
  • 1
  • 53
  • 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,609
  • 2
  • 20
  • 34
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,456
  • 4
  • 45
  • 84