Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
java_language_specification_7.pdf
Скачиваний:
13
Добавлен:
21.03.2016
Размер:
3.11 Mб
Скачать

EXPRESSIONS

Argument Lists are Evaluated Left-to-Right 15.7.4

15.7.4 Argument Lists are 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.

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

Example 15.7.4-1. Evaluation Order At Method Invocation

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

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;

}

static int oops() throws Exception { throw new Exception("oops");

}

}

This program produces the output:

java.lang.Exception: oops, id=1

427

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]