I\'ve noticed JLS talks of 5.1.10 Capture Conversion, but I fail to understand what they are.
Can anyone explain them to me/give examples?
A parameterized type involving wildcard type arguments is really a union type. For example
List extends Number> = Union{ List | S <: Number }
In 2 cases, instead of using List extends Number>
, Java uses the captured version List
, where S is a just-created type variable with upper bound Number
.
(1) http://java.sun.com/docs/books/jls/third_edition/html/expressions.html
To narrow the type of an expression. If an expression's type is List extends Number>
, we know for sure that the runtime type of the object is actually a List
for some concrete type S (S <: Number>
). So compiler uses List
instead, to perform more accurate type analysis.
Capture conversion is applied to each expression individually; this leads to some dumb results:
void test1(List a){}
void test2(List a, List b){}
List> x = ...;
test1(x); // ok
test2(x, x); // error
(2) http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.10.2
In subtype checking A :< B
where A
involves wildcard arguments. For example,
List extends Number> :< B
<=>
Union{ List | S <: Number} :< B
<=>
List :< B, for all S <: Number
So in effect, we are checking the captured version of type A