x++ is an expression. Expressions are 'resolved' (calculated); in other words, they resolve into a value. For example, the expression 5 + 2, if resolved, resolves as value 7.
x++ is a bit of a weird expression; it's one that has side effects. It doesn't just calculate, the act of resolving it, all by itself, causes things to occur. Thus, it does 2 things: It resolves into a value, and it causes other stuff to occur as well.
Specifically, x++ means: Return the value of x as it is right now. However, as a side effect, increment x (but, the value of this expression is x as it was before you increment it).
x = someExpr is java-ese for: Resolve expression someExpr, then set x to have the calculated value.
Thus, x = x++ is silly code that you never want to write. Let's say x is 5 when we start this, then:
- First, we 'resolve'
x++, meaning, the value is going to be 5.
- But before we're actually done, increment x.
x now holds the value of 6, but the expression's value is 5.
- Now overwrite whatever
x was holding with the value of what the expression is, which is 5.
Thus, x is... still 5.
If you want to increment x and that's all, just :
public static int getX(int x) {
x++;
return x;
}
or even simpler:
public static int getX(int x) {
return x + 1;
}
is what you want.