EXPRESSIONS
Argument Lists are Evaluated Left to Right
15.6.4
class Test {
public static void main(String[] args) {
double d = 8e+307;
System.out.println(4.0 * d * 0.5);
System.out.println(2.0 * d);
}
}
prints:
Infinity
1.6e+308
because the first expression overflows and the second does not.
In contrast, integer addition and multiplication
are
provably associative in
Java; for example
a+b+c
, where
a
,
b
, and
c
are local variables (this simplifying
assumption avoids issues involving multiple threads and
volatile
variables),
will always produce the same answer whether evaluated as
(a+b)+c
or
a+(b+c)
;
if the expression
b+c
occurs nearby in the code, a smart compiler may be able to
use this common subexpression.
15.6.4 Argument Lists are Evaluated Left to Right
In a method or constructor invocation or class instance creation expression, argu
ment expressions may appear within the parentheses, separated by commas. Each
argument expression appears to be fully evaluated before any part of any argument
expression to its right.
Thus:
class Test {
public static void main(String[] args) {
String s = "going, ";
print3(s, s, s = "gone");
}
static void print3(String a, String b, String c) {
System.out.println(a + b + c);
}
}
always prints:
going, going, gone
because the assignment of the string
"gone"
to
s
occurs after the first two argu
ments to
print3
have been evaluated.
309
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved