-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...

3
  • exact same way it works in other languages Commented Dec 16, 2013 at 14:55
  • This page can. Commented Dec 16, 2013 at 14:56
  • x is being incremented, however after being incremented it is being set back to the old value. Commented Dec 16, 2013 at 14:59

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.