Content deleted Content added
→Wildcard as parameter type: fix code |
→Wildcard as parameter type: contrast with concrete parameterization |
||
Line 9:
== Wildcard as parameter type ==
In the body of a generic unit, the (formal) type parameter is handled like its [[bounded quantification|upper bound]] (expressed with <code>'''extends'''</code>; <code>Object</code> if not constrained). If the return type of a method is the type parameter, the result (e.g. of type <code>?</code>) can be referenced by a variable of the type of the upper bound (or <code>Object</code>). In the other direction, the wildcard fits no other type, not even <code>Object</code>: If <code>?</code> has been applied as the formal type parameter of a method, no actual parameters can be passed to it.
<source lang="java5">
Line 22:
}
...
Generic<
Generic<?> wildcardReference = concreteTypeReference;
UpperBound ub = wildcardReference.read(); // Object would also be OK
wildcardReference.write(new Object()); // type error
wildcardReference.write(new UpperBound()); // type error
</source>
|