unable to make out this assignment in java

前端 未结 4 2119
长发绾君心
长发绾君心 2020-12-06 18:32

can anybody explain this why its happening

int i=0;
i=i++;
i=i++;
i=i++;
System.out.println(i);

it prints zero.

4条回答
  •  臣服心动
    2020-12-06 19:07

    First thing is you should not write this kind of code....

    But if we consider for the questions sake then this simple: It has to do with the way the postfix operator "returns" the value. A postfix has precedence over the assignment operator, but the postfix operator after incrementing the value of i returns previous value of i. So i get again assigned to its previous value.

    And once again don't use this construct in your code as the next programmer who sees this will come after you (with something big in his hands) :)

提交回复
热议问题