i++
is an assignment to a variable i
.
In your case, zero++
is an equivalent to zero = zero + 1
. So 0++
would mean 0 = 0 + 1
, which makes no sense, as well as getInt() = getInt() + 1
.
More accurately :
int oneA = zero++;
means
int oneA = zero;
zero = zero + 1; // OK, oneA == 0, zero == 1
int oneB = 0++;
means
int oneB = 0;
0 = 0 + 1; // wrong, can't assign value to a value.
int oneC = getInt()++;
means
int oneC = getInt();
getInt() = getInt() + 1; // wrong, can't assign value to a method return value.
From a more general point of view, a variable is a L-value, meaning that it refers to a memory location, and can therefore be assigned. L in L-value stands for left side of the assignment operator (i.e. =
), even if L-values can be found either on the left side or the right side of the assignment operator (x = y
for instance).
The opposite is R-value (R stands for right side of the assignment operator). R-values can be used only on the right side of assignment statements, to assign something to a L-value. Typically, R-values are literals (numbers, characters strings...) and methods.
int oneA = zero++;
assigns one tooneA
. This is incorrect. The post increment operator returns the old (non-incremented) value. Afterint oneA = zero++;
,oneA
is0
andzero
is1
. – Empiricint oneC = getInt() + 1;
? – Z++
Once solution done it looks simple. – Madea++
means. If you ask yourself "why is there a++
, anyway, when+1
is just as easy to write?", and can answer correctly, then you already know the problem with the code in this question. – Newsprint