Java Reference
In-Depth Information
15.7.4. Argument Lists are Evaluated Left-to-Right
In a method or constructor invocation or class instance creation expression, argument ex-
pressions may appear within the parentheses, separated by commas. Each argument expres-
sion appears to be fully evaluated before any part of any argument expression to its right.
If evaluation of an argument expression completes abruptly, no part of any argument ex-
pression to its right appears to have been evaluated.
Example 15.7.4-1. Evaluation Order At Method Invocation
Click here to view code image
class Test1 {
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);
}
}
This program produces the output:
going, going, gone
because the assignment of the string “ gone ” to s occurs after the first two arguments to
print3 have been evaluated.
Example 15.7.4-2. Abrupt Completion of Argument Expression
Click here to view code image
class Test2 {
static int id;
public static void main(String[] args) {
try {
test(id = 1, oops(), id = 3);
} catch (Exception e) {
System.out.println(e + ", id=" + id);
}
}
static int test(int a, int b, int c) {
return a + b + c;
}
Search WWH ::




Custom Search