An expression produces a result
and returns a value. Examples include:
- i = 2
: the assignment puts 2 into the i variable and returns
the value 2
- k++ :
returns k, then k is incremented by 1
- x < y
: logical "less than" comparison, returns a
Boolean true or false value
- i | j
: returns the value of a bitwise OR operation on bits in
the two variables.
- 4.0*Math.sin(i
* Math.PI) :
combines several operations in this expression including multiplication
& a method call.
Expressions involve at least one operator. A single operator can
have 1, 2 or 3 operands.
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 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)
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.
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.
Returned Value
A value is returned from the expression. The following statements
use the assignment operator "=" and the addition operator "+"
int
x = 3;
int y = x + 5;
These statements result in x holding the value 3 and y holding
the value 8. The entire expression y
= x + 5 could be used in another expression:
int
x = 3;
int y;
int z = (y = x + 5) * 4;
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.
Expression Evaluation
The operands of an operator are always evaluated left to right.
For example, in
x
= a + b;
the "+" operator will determine the value of expression a and then
expression b. Do not get this rule confused with the precedence
and associativity rules, discussed next.
Precedence determines the order in which operators act in an expression
with more than one operator. The Operator
Precedence Table gives the precedence rating for each operator,
the higher number indicating higher precedence.
Associativity rules determines
how the compiler groups the operands and operators in an expression
with more than one operator of the same precedence. For example,
in the expression
x
= a + b * c;
the evaluation begins with a and then the "+" operator determines
its right operand. But in this case the right operand consists of
the expression "b * c". The multiplication operator "*" has a higher
precedence than the additive operator "+" so b multiplies c rather
than sums with a.
Precedence can be overridden with parentheses, as in
x
= (a + b) * c;
The parentheses force the addition of b to a, and then c multiplies
this sum.
Although the precedence ratings, which are similar to those in
C/C++, were chosen for the most "natural" ordering of the operator
evaluations, it never hurts to use the parentheses if you are unsure
of the precedence and to make the code more readable.
When the operations in an expression all have the same precedence
rating, the associativity rules determine the order of the operations.
For most operators, the evaluation is done left to right, as in
x
= a - b + c;
Here, addition and subtraction have the same precedence rating
and so a and b are subtracted and then c added to the difference.
Again, parentheses can be used to overrule the default associativity,
as in
x
= a - (b + c);
The assignment and unary operators, on the other hand, are associated
right to left. For example, the statement
x
+= y -= -~4;
is equivalent to
x
+= (y -= (-(~4)));
or, in long hand,
int
a = ~4;
a = -a;
y = y - a;
x = x + y;
See the Operators section for more
about expressions.
Latest update: Oct. 14, 2004
|