Java Reference
In-Depth Information
System.out.println(b);
}
}
This program produces the output:
12
12
It is not permitted for either assignment (compound for
a
, simple for
b
) to produce the
result
6
.
Example 15.7.1-3. Abrupt Completion of Evaluation of the Left-Hand Operand
class Test3 {
public static void main(String[] args) {
int j = 1;
try {
int i = forgetIt() / (j = 2);
} catch (Exception e) {
System.out.println(e);
System.out.println("Now j = " + j);
}
}
static int forgetIt() throws Exception {
throw new Exception("I'm outta here!");
}
}
This program produces the output:
java.lang.Exception: I'm outta here!
Now j = 1
That is, the left-hand operand
forgetIt()
of the operator
/
throws an exception before the
right-hand operand is evaluated and its embedded assignment of
2
to
j
occurs.
15.7.2. Evaluate Operands before Operation
The Java programming language guarantees that every operand of an operator (except the
conditional operators
&&
,
||
, and
? :
) appears to be fully evaluated before any part of the
operation itself is performed.