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.

15.7.4 Argument Lists are Evaluated Left-to-Right EXPRESSIONS<br />

418<br />

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

strictfp class Test {<br />

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

double d = 8e+307;<br />

System.out.println(4.0 * d * 0.5);<br />

System.out.println(2.0 * d);<br />

}<br />

}<br />

prints:<br />

Infinity<br />

1.6e+308<br />

because the first expression overflows and the second does not.<br />

In contrast, integer addition and multiplication are provably associative in the<br />

<strong>Java</strong> programming language.<br />

For example a+b+c, where a, b, and c are local variables (this simplifying<br />

assumption avoids issues involving multiple threads and volatile variables),<br />

will always produce the same answer whether evaluated as (a+b)+c or a+(b+c);<br />

if the expression b+c occurs nearby in the code, a smart compiler may be able to<br />

use this common subexpression.<br />

15.7.4 Argument Lists are Evaluated Left-to-Right<br />

In a method or constructor invocation or class instance creation expression, argument<br />

expressions may appear within the parentheses, separated by commas. Each<br />

argument expression appears to be fully evaluated before any part of any argument<br />

expression to its right.<br />

Thus:<br />

class Test {<br />

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

String s = "going, ";<br />

print3(s, s, s = "gone");<br />

}<br />

static void print3(String a, String b, String c) {<br />

System.out.println(a + b + c);<br />

}<br />

}<br />

always prints:<br />

going, going, gone<br />

because the assignment of the string "gone" to s occurs after the first two arguments<br />

to print3 have been evaluated.<br />

DRAFT

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

Saved successfully!

Ooh no, something went wrong!