Java Reference
In-Depth Information
count
currently contains the value
15
, the following statement assigns the value
15
to
total
and the value
16
to
count
:
total = count++;
However, the following statement assigns the value
16
to both
total
and
count
:
total = ++count;
The value of
count
is incremented in both situations, but the value used in the
larger expression depends on whether a prefix or postfix form of the increment
operator is used.
Because of the subtle differences between the prefix and postfix forms of the
increment and decrement operators, they should be used with care. As always,
favor the side of readability.
As a convenience, several
assignment operators
have been defined in Java that
combine a basic operation with assignment. For example, the
+=
operator can be
used as follows:
total += 5;
This performs the same operation as the following statement:
total = total + 5;
The right-hand side of the assignment operator can be a full expression. The
expression on the right-hand side of the operator is evaluated, then that result is
added to the current value of the variable on the left-hand side, and that value is
stored in the variable. Therefore, the following statement:
total += (sum - 12) / count;
is equivalent to:
total = total + ((sum - 12) / count);
Many similar assignment operators are defined in Java, including those that
perform subtraction (
-=
), multiplication (
*=
), division (
/=
), and remainder (
%=
).
The entire set of Java operators is discussed in Appendix D.
All of the assignment operators evaluate the entire expression on the right-
hand side first, then use the result as the right operand of the other operation.
Therefore, the following statement:
result *= count1 + count2;
Search WWH ::
Custom Search