3

I was asked in an interview the below question.

int b = 0;
b = b++;
b = b++;
b = b++;
b = b++;

what will be the value of b after every line execution ? The output is coming as 0 for every line.

Why is the output not coming as 0,1,2,3 ?

robin
  • 1,893
  • 1
  • 18
  • 38

4 Answers4

6

In Java, the expression

b = b++

is equivalent to

int tmp = b;
b = b + 1;
b = tmp;

Hence the result.

(In some other languages, the exact same expression has unspecified behaviour. See Undefined behavior and sequence points.)

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

Because this is the order of execution of b = b++:

  1. Get the value of b into some temp (probably into a bytecode register); this is the first part of b++, since it's a post-increment
  2. Increment and store the incremented result in b (the second part of b++)
  3. Assign the value from Step 1 to b (the result of the =)
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • From what I see in byte code - `2: iload_1 m 3: iinc , 6: istore_1` are called one after another. So, the value of `b` is first loaded into the stack (from the local variable array), then the value of b is fetched, incremented and written back to variable b. So, here b will be 1. then istore_1 is called which will pop the value of b from stack and overwrite the variable b (local variable array) with current value on stack (0). So, in m particular implementation, at byte-code level itself, the values are being *overwritten* :) – TheLostMind Dec 23 '14 at 13:38
  • @TheLostMind: Yes, first written in #2 above, then overwritten in #3 above. I suspect if it were a hotspot, HotSpot would...fix that... – T.J. Crowder Dec 23 '14 at 13:40
  • What would the hot spot fix in this one?. I am merely curious. Will it *skip* the `iinc` ? – TheLostMind Dec 23 '14 at 13:42
  • @TheLostMind: I don't know much about HotSpot's internals, but HotSpot's job is to make hotspots in the code faster. Here, we clearly have a dead store (`iinc` followed by `istore_1`), so... And who knows, perhaps it could even turn the whole thing into a no-op, since that's what it is. – T.J. Crowder Dec 23 '14 at 13:49
  • may be. I have a feeling that the latest hot spots are *this* smart :).. Thanks – TheLostMind Dec 23 '14 at 13:51
3

Hint:

int b = 0, c;
c = b++;
c = b++;
c = b++;
c = b++;
System.out.println(c);

c now will be 3 like you thought, but because in your question you're assigning b, it'll get 0, because as already explained, it's the same as:

int tmp = b;
b = b + 1;
b = tmp;
Maroun
  • 94,125
  • 30
  • 188
  • 241
1

b++ is the same as:

int temp = b;
b = b + 1;
return temp;

As you can see b++ will return his old value, but overwrites the value of b. But since you assign the returned (old) value to b, the new value will be overwritten and therefore "ignored".

A difference would be, if you write:

b = ++b; //exactly the same as just "++b"

This does the incrementation first and the new value will be returned.

Tom
  • 16,842
  • 17
  • 45
  • 54