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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

EXPRESSIONS Evaluation Respects Parentheses and Precedence 15.7.3<br />

int i = 1 / (divisor * loseBig());<br />

} catch (Exception e) {<br />

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

}<br />

}<br />

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

throw new Exception("Shuffle off to Buffalo!");<br />

}<br />

}<br />

always prints:<br />

java.lang.Exception: Shuffle off to Buffalo!<br />

and not:<br />

java.lang.ArithmeticException: / by zero<br />

since no part of the division operation, including signaling of a divide-by-zero<br />

exception, may appear to occur before the invocation of loseBig completes, even<br />

though the implementation may be able to detect or infer that the division operation<br />

would certainly result in a divide-by-zero exception.<br />

15.7.3 Evaluation Respects Parentheses and Precedence<br />

<strong>Java</strong> programming language implementations must respect the order of evaluation<br />

as indicated explicitly by parentheses and implicitly by operator precedence. An<br />

implementation may not take advantage of algebraic identities such as the associative<br />

law to rewrite expressions into a more convenient computational order unless<br />

it can be proven that the replacement expression is equivalent in value and in its<br />

observable side effects, even in the presence of multiple threads of execution<br />

(using the thread execution model in §17), for all possible computational values<br />

that might be involved.<br />

In the case of floating-point calculations, this rule applies also for infinity and<br />

not-a-number (NaN) values. For example, !(x=y,<br />

because these expressions have different values if either x or y is NaN or both are<br />

NaN.<br />

Specifically, floating-point calculations that appear to be mathematically associative<br />

are unlikely to be computationally associative. Such computations must<br />

not be naively reordered.<br />

For example, it is not correct for a <strong>Java</strong> compiler to rewrite 4.0*x*0.5 as<br />

2.0*x; while roundoff happens not to be an issue here, there are large values of x<br />

for which the first expression produces infinity (because of overflow) but the second<br />

expression produces a finite result.<br />

DRAFT<br />

417

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

Saved successfully!

Ooh no, something went wrong!