37

Possible Duplicate:
What is x after “x = x++”?

int y = 1;
y = y++;
System.out.println("Value Of y :"+y);

The output is Value Of y : 1

I am incrementing the y and assigning that to "y" again. y++ hence first the value is used and then it increments the value. Hence it assign the value and it gets incremented. So I thought the output would be 2.

int y = 1;
int x = y++;
System.out.println("Value Of x :"+x);
System.out.println("Value Of y :"+y);

The output is:
Value Of x : 1
Value Of y : 2

In this case it is working. I mean the way I thought is correct.

Can any one please explain to me why?

Community
  • 1
  • 1
Chandana
  • 443
  • 5
  • 11

0 Answers0