Java Reference
In-Depth Information
If the operator is a compound-assignment operator (ยง 15.26.2 ) , then evaluation of the left-
hand operand includes both remembering the variable that the left-hand operand denotes
and fetching and saving that variable's value for use in the implied binary operation.
If evaluation of the left-hand operand of a binary operator completes abruptly, no part of
the right-hand operand appears to have been evaluated.
Example 15.7.1-1. Left-Hand Operand Is Evaluated First
In the following program, the * operator has a left-hand operand that contains an as-
signment to a variable and a right-hand operand that contains a reference to the same
variable. The value produced by the reference will reflect the fact that the assignment
occurred first.
Click here to view code image
class Test1 {
public static void main(String[] args) {
int i = 2;
int j = (i=3) * i;
System.out.println(j);
}
}
This program produces the output:
9
It is not permitted for evaluation of the * operator to produce 6 instead of 9 .
Example 15.7.1-2. Implicit Left-Hand Operand In Operator Of Compound
Assigment
In the following program, the two assignment statements both fetch and remember the
value of the left-hand operand, which is 9 , before the right-hand operand of the addi-
tion operator is evaluated, at which point the variable is set to 3 .
Click here to view code image
class Test2 {
public static void main(String[] args) {
int a = 9;
a += (a = 3); // first example
System.out.println(a);
int b = 9;
b = b + (b = 3); // second example
Search WWH ::




Custom Search