-8

Can some one explain me how post increment works in Java ?

 public static void main(String[] args) {
            int a = 10;
            /*
             *  1. "a" is assigned to "b"
             *  2. Then a gets incremented by 1
             */
            int b = a++;
            System.out.println(b);  //prints 10
            System.out.println(a);  //prints 11


            int x = 10;
            /*
             *  1. "x" is assigned to "x"
             *  2. Then "x" is not getting incremented by 1
             */
            x = x++;
            System.out.println(x);  //prints 10
        }

So when we have same variable on both sides result is different. Please explain...

1 Answers1

0

This is because even though you increment x , you are assigning x an older value (in this case 10)

user1801279
  • 1,743
  • 5
  • 24
  • 40