Java Reference
In-Depth Information
String id(String x) { return x; }
}
Now, given an invocation:
C c = new D();
c.id(new Object()); // fails with a ClassCastException
The erasure of the actual method being invoked,
D.id()
, differs in its signature from
that of the compile-time method declaration,
C.id()
. The former takes an argument of
type
String
while the latter takes an argument of type
Object
. The invocation fails with
a
ClassCastException
before the body of the method is executed.
Such situations can only arise if the program gives rise to a compile-time unchecked
Implementations can enforce these semantics by creating
bridge methods
. In the
above example, the following bridge method would be created in class
D
:
Object id(Object x) { return id((String) x); }
This is the method that would actually be invoked by the Java Virtual Machine in re-
sponse to the call
c.id(new Object())
shown above, and it will execute the cast and fail,
as required.
15.13. Array Access Expressions
An array access expression refers to a variable that is a component of an array.
ArrayAccess:
ExpressionName
[
Expression
]
PrimaryNoNewArray
[
Expression
]
An array access expression contains two subexpressions, the
array reference expression
(before the left bracket) and the
index expression
(within the brackets).
Note that the array reference expression may be a name or any primary expression
The type of the array reference expression must be an array type (call it
T
[]
, an array whose
components are of type
T
), or a compile-time error occurs.
The index expression undergoes unary numeric promotion (§
5.6.1
). The promoted type
must be
int
, or a compile-time error occurs.