10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

15.13 Array Access Expressions EXPRESSIONS<br />

482<br />

System.out.println(((T1)this).s());<br />

}<br />

}<br />

class Test {<br />

public static void main(String[] args) {<br />

T3 t3 = new T3();<br />

t3.test();<br />

}<br />

}<br />

which produces the output:<br />

s()= 3<br />

super.s()= 2<br />

((T2)this).s()= 3<br />

((T1)this).s()= 3<br />

<strong>The</strong> casts to types T1 and T2 do not change the method that is invoked,<br />

because the instance method to be invoked is chosen according to the run-time<br />

class of the object referred to be this. A cast does not change the class of an<br />

object; it only checks that the class is compatible with the specified type.<br />

15.13 Array Access Expressions<br />

An array access expression refers to a variable that is a component of an array.<br />

ArrayAccess:<br />

ExpressionName [ Expression ]<br />

PrimaryNoNewArray [ Expression ]<br />

An array access expression contains two subexpressions, the array reference<br />

expression (before the left bracket) and the index expression (within the brackets).<br />

Note that the array reference expression may be a name or any primary expression<br />

that is not an array creation expression (§15.10).<br />

<strong>The</strong> type of the array reference expression must be an array type (call it T[],<br />

an array whose components are of type T ) or a compile-time error results. <strong>The</strong>n<br />

the type of the array access expression is the result of applying capture conversion<br />

(§5.1.10) to T.<br />

<strong>The</strong> index expression undergoes unary numeric promotion (§); the promoted<br />

type must be int.<br />

<strong>The</strong> result of an array reference is a variable of type T, namely the variable<br />

within the array selected by the value of the index expression. This resulting variable,<br />

which is a component of the array, is never considered final, even if the<br />

array reference was obtained from a final variable.<br />

DRAFT

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!