15.6.4 Argument Lists Evaluated Left-to-Right

In a method or constructor invocation or class instance creation expression, argument 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 arguments to print3 have been evaluated.

If evaluation of an argument expression completes abruptly, no part of any argument expression to its right appears to have been evaluated.

Thus, the example:


class Test {
	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 oops() throws Exception { throw new Exception("oops"); }
static int test(int a, int b, int c) { return a + b + c; }
}

prints:

java.lang.Exception: oops, id=1

because the assignment of 3 to id is not executed.