The difference in behavior is due to the behavior of two different operators that you are using. The ++
in front of the variable is a prefix operator and the ++
after the variable is a postfix operator. The two different operators modify the variable at two different times during the evaluation of the expression. See this stackoverflow What is the difference between prefix and postfix operators?
When you do an assignment, the operations on the right side of the assignment operator (the equals sign or =
) are evaluated and the result is put into the variable on the left side of the assignment operator.
The operations on the right side are evaluated or computed by using various rules of operator precedence. See this stackoverflow Operator precedence in Java.
So the question is when the right side is evaluated, what is the result of the various operations that are to the right side of the assignment operator.
Both the prefix and the postfix operators have almost the highest operator precedence in Java as well as most languages. This means that unless parenthesis are used to modify the sequence of evaluation, prefix and postfix operators will be one of the first operators evaluated. See Java Operator Precedence Table for a short and simple cheatsheet table.
The prefix operator modifies the variable and then the new value of the variable is used in any additional operations. The postfix operator modifies the variable after the current value of the variable is saved as a temporary value.
In other words, the value of a variable used in an expression will be a changed value with a prefix operator and an unchanged value with a postfix operator.
It is easier to see what is happening by using two variables rather than one:
int a, b;
a=0;
b = ++a; // increment a and assign the new value of variable a to variable b
b = a++; // increment a and assign the old value of variable a to variable b
If you use the single variable as you are doing then what happens is that the value of the variable a
is changed by the assignment statement to be whatever the right hand side evaluates to. So even though the variable a
is modified by the prefix and the postfix operators, the assignment will overwrite the new value with what ever the right hand side evaluates to.
With the prefix operator, the right hand side evaluates to the new, incremented value of the variable a
. With the postfix operator, the right hand side evaluates to the old value of the variable a
before it is incremented by the postfix operator.
Often times when the desire is to just increment a variable, a prefix or postfix ++
is used in a stand alone statement.
int a = 0;
a = a + 1; // increment a by adding 1 to the value and assigning new value to a
a += 1; // increment a by using += operator
a++; // increment a by using postfix ++ operator
++a; // increment a by using the prefix ++ operator
In the statements above where the ++
operator is used nothing is done with the result of the operation. So the variable a
is incremented however the old value of a
from the postfix operator or the new value of a
from the prefix operator are not used for anything. The only result of using these operators is that the variable a
is incremented.
In other words when a prefix or postfix operator is used there are actually two outcomes or results from the operator. The first result is the change in the variable that is being modified by the operator, also known as a side effect. The second result is the value that is provided to any other operators that may be involved in the calculation. In the case of the prefix operator, this second result is the new value of the variable after the prefix operator is applied. In the case of the postfix operator, this second result is the old value of the variable before the postfix operator is applied.