Possible Duplicate:
post increment operator java
What is x after “x = x++”?
Can somebody explain me the result of each one of the following small programs? :
public static void main(String[] args) {
int a = 10;
a = a++;
System.out.println(a);
}
The result is: 10
Why not 11 since a should have been increased after the assignment? Is that the fact that it comes to different variables left and right of the opeartor = ?
The next one:
public static void main(String[] args) {
int a = 10;
a = ++a;
System.out.println(a);
}
The result is: 11
Comprehensible, but the compiler presents the warning: "The assignment to variable a has no effect". The result dissents though.
Update:
I do not modify my original question but I add this comment to clarify that now I catch the meaning of the warning. That is, even without the assignment (by a plain statement ++a) the result would be the same (11).