Java: Prefix/postfix of increment/decrement operators?

前端 未结 10 1409
不知归路
不知归路 2020-11-22 07:17

From the program below or here, why does the last call to System.out.println(i) print the value 7?

class PrePostDemo {
     public          


        
10条回答
  •  既然无缘
    2020-11-22 07:34

    Maybe you can understand better Prefix/postfix with this example.

    public class TestPrefixPostFix 
    {
        public static void main (String[] args)
        { 
            int x=10;
            System.out.println( (x++ % 2 == 0)?"yes "+ x: " no "+x);
            x=10;
            System.out.println( (++x % 2 == 0)?"yes "+ x: " no "+x);
        }
    }    
    

提交回复
热议问题