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

EXPRESSIONS

Expression

15.27

the evaluation of the right-hand operand indeed occurs after the checks for a null array

 

reference value and an out-of-bounds index value.

 

Example 15.26.2-2. Value Of Left-Hand Side Of Compound Assignment Is Saved Before

 

Evaluation Of Right-Hand Side

 

class Test {

 

public static void main(String[] args) {

 

 

int k = 1;

 

 

int[] a = { 1 };

 

 

k += (k = 4) * (k + 2);

 

 

a[0] += (a[0] = 4) * (a[0] + 2);

 

 

System.out.println("k==" + k + " and a[0]==" + a[0]);

 

}

 

 

}

 

 

This program produces the output:

 

k==25 and a[0]==25

 

The value 1 of k is saved by the compound assignment operator += before its right-hand

 

operand (k =

4) * (k + 2) is evaluated. Evaluation of this right-hand operand then

 

assigns 4 to k, calculates the value 6 for k + 2, and then multiplies 4 by 6 to get 24. This is added to the saved value 1 to get 25, which is then stored into k by the += operator. An identical analysis applies to the case that uses a[0].

In short, the statements:

k += (k = 4) * (k + 2);

a[0] += (a[0] = 4) * (a[0] + 2);

behave in exactly the same manner as the statements:

k = k + (k = 4) * (k + 2);

a[0] = a[0] + (a[0] = 4) * (a[0] + 2);

15.27 Expression

An Expression is any assignment expression:

Expression:

AssignmentExpression

Unlike C and C++, the Java programming language has no comma operator.

535

15.28 Constant Expressions EXPRESSIONS

15.28 Constant Expressions

ConstantExpression:

Expression

A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

Literals of primitive type and literals of type String (§3.10.1, §3.10.2, §3.10.3, §3.10.4, §3.10.5)

Casts to primitive types and casts to type String (§15.16)

The unary operators +, -, ~, and ! (but not ++ or --) (§15.15.3, §15.15.4, §15.15.5, §15.15.6)

The multiplicative operators *, /, and % (§15.17)

The additive operators + and - (§15.18)

The shift operators <<, >>, and >>> (§15.19)

The relational operators <, <=, >, and >= (but not instanceof) (§15.20)

The equality operators == and != (§15.21)

The bitwise and logical operators &, ^, and | (§15.22)

The conditional-and operator && and the conditional-or operator || (§15.23, §15.24)

The ternary conditional operator ? : (§15.25)

Parenthesized expressions (§15.8.5) whose contained expression is a constant expression.

Simple names (§6.5.6.1) that refer to constant variables (§4.12.4).

Qualified names (§6.5.6.2) of the form TypeName . Identifier that refer to constant variables (§4.12.4).

Compile-time constant expressions of type String are always "interned" so as to share unique instances, using the method String.intern.

A compile-time constant expression is always treated as FP-strict (§15.4), even if it occurs in a context where a non-constant expression would not be considered to be FP-strict.

536

EXPRESSIONS

Constant Expressions

15.28

Compile-time constant expressions are used in case labels in switch statements (§14.11) and have a special significance for assignment conversion (§5.2) and initialization of a class or interface (§12.4.2). They may also govern the ability of a while, do, or for statement to complete normally (§14.21), and the type of a conditional operator ? : with numeric operands.

Example 15.28-1. Constant Expressions

true (short)(1*2*3*4*5*6) Integer.MAX_VALUE / 2 2.0 * Math.PI

"The integer " + Long.MAX_VALUE + " is mighty big."

537

15.28

Constant Expressions

EXPRESSIONS

538

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