Java Reference
In-Depth Information
This results in
y
holding 8 and
z
holding 32. The assignment operator
“=
„
in
the expression
(y = x+5)
produces a new value for
y
and also returns the value of
y
to be used in the
expression for
z
.
2.8.3 Effects on operands
In most of the operations, the operands themselves are not changed. However,
for some operators, the operand(s) do undergo a change:
Assignment operators
-
“
x
=
y
„
replaces the value of the first operand with that of
the second. The other assignment operators,
“
*
=
,
+
=
,
-
=
,
/
=
„
, also replace the value
of the first operand but only after using its initial value in the operation indicated by the
symbol before the equals sign. For example,
x*
=
y
results in
x
being replaced by
x*y
. Also, this is the value returned from the operation.
Increment and decrement operators
(++x)
-
x
is incremented before its value is returned.
-
x
is decremented before its value is returned.
(--x)
- the initial value of
x
is returned and then
x
is incremented.
(x++)
- the initial value of
x
is returned and then
x
is decremented.
(x--)
For the increment and decrement operations, note that in a standalone expression
such as
x++;
there is no effective difference between
x++
and
++x
. Both expressions increment
the value stored in the variable
x
.However, in expressions such as
y
=
x++;
and
z
=
++i;
the order of the appearance of the increment operator is important. In the former
case,
y
takes on the value of
x
before
the increment occurs. If
x
is initially 3, then
y
becomes 3 and
x
becomes 4. In the latter case the increment occurs before the
value is used. So an initial value of 3 for
i
leads to
i
incrementing to 4 and then
z
taking on the new value, 4.
Remember that if an operand is changed by the operation and the statement
holding that expression is processed again, as in a loop, the operand's value will
be different for each pass.
Search WWH ::
Custom Search