10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

15.7.2 Evaluate Operands before Operation EXPRESSIONS<br />

416<br />

evaluated, thereby setting the variable to 3. It is not permitted for either example<br />

to produce the result 6. Note that both of these examples have unspecified behavior<br />

in C, according to the ANSI/ISO standard.<br />

If evaluation of the left-hand operand of a binary operator completes abruptly,<br />

no part of the right-hand operand appears to have been evaluated.<br />

Thus, the test program:<br />

class Test {<br />

public static void main(String[] args) {<br />

int j = 1;<br />

try {<br />

int i = forgetIt() / (j = 2);<br />

} catch (Exception e) {<br />

System.out.println(e);<br />

System.out.println("Now j = " + j);<br />

}<br />

}<br />

static int forgetIt() throws Exception {<br />

throw new Exception("I’m outta here!");<br />

}<br />

}<br />

prints:<br />

java.lang.Exception: I'm outta here!<br />

Now j = 1<br />

That is, the left-hand operand forgetIt() of the operator / throws an exception<br />

before the right-hand operand is evaluated and its embedded assignment of 2<br />

to j occurs.<br />

15.7.2 Evaluate Operands before Operation<br />

DRAFT<br />

<strong>The</strong> <strong>Java</strong> programming language also guarantees that every operand of an operator<br />

(except the conditional operators &&, ||, and ?:) appears to be fully evaluated<br />

before any part of the operation itself is performed.<br />

If the binary operator is an integer division / (§15.17.2) or integer remainder<br />

% (§15.17.3), then its execution may raise an ArithmeticException, but this<br />

exception is thrown only after both operands of the binary operator have been<br />

evaluated and only if these evaluations completed normally.<br />

So, for example, the program:<br />

class Test {<br />

public static void main(String[] args) {<br />

int divisor = 0;<br />

try {

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!