Java Reference
In-Depth Information
This program produces the output:
k==25 and a[0]==25
The value
1
of
k
is saved by the compound assignment operator
+=
before its right-
hand operand
(k = 4) * (k + 2)
is evaluated. Evaluation of this right-hand operand then
assigns
4
to
k
, calculates the value
6
for
k + 2
, and then multiplies
4
by
6
to get
24
. This
is added to the saved value
1
to get
25
, which is then stored into
k
by the
+=
operator.
An identical analysis applies to the case that uses
a[0]
.
In short, the statements:
k += (k = 4) * (k + 2);
a[0] += (a[0] = 4) * (a[0] + 2);
behave in exactly the same manner as the statements:
k = k + (k = 4) * (k + 2);
a[0] = a[0] + (a[0] = 4) * (a[0] + 2);
15.27. Expression
An
Expression
is any assignment expression:
Expression:
AssignmentExpression
Unlike C and C++, the Java programming language has no comma operator.
15.28. Constant Expressions
ConstantExpression:
Expression
A compile-time
constant expression
is an expression denoting a value of primitive type or
a
String
that does not complete abruptly and is composed using only the following: