unable to make out this assignment in java

前端 未结 4 2107
长发绾君心
长发绾君心 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条回答
  •  Happy的楠姐
    2020-12-06 19:07

    Let I=++, an increase, and A=i, an assignment. They are non-commutative: IA != AI.

    Summary

    IA = "first increase then assignment"

    AI="first assignment then increase"

    Counter-Example

    $ javac Increment.java 
    $ java Increment 
    3
    $ cat Increment.java 
    import java.util.*;
    import java.io.*;
    
    public class Increment {
        public static void main(String[] args) {
            int i=0;
            i=++i;
            i=++i;
            i=++i;
            System.out.println(i);
        }
    }
    

    Related

    • Initialization, assignment and declaration

提交回复
热议问题