Java Reference
In-Depth Information
Here, the method is declared within the interface Collection<E> , and is designed to add
all the elements of its incoming argument to the collection upon which it is invoked.
A natural tendency would be to use Collection<E> as the type of c , but this is unneces-
sarily restrictive. An alternative would be to declare the method itself to be generic:
<T> boolean addAll(Collection<T> c)
This version is sufficiently flexible, but note that the type parameter is used only once
in the signature. This reflects the fact that the type parameter is not being used to ex-
press any kind of interdependency between the type(s) of the argument(s), the return
type and/or throws type. In the absence of such interdependency, generic methods are
considered bad style, and wildcards are preferred.
Reference(T referent, ReferenceQueue<? super T> queue);
Here, the referent can be inserted into any queue whose element type is a supertype of
the type T of the referent; T is the lower bound for the wildcard.
Two type arguments are provably distinct if one of the following is true:
• Neither argument is a type variable or wildcard, and the two arguments are not the
same type.
• One type argument is a type variable or wildcard, with an upper bound (from cap-
ture conversion, if necessary) of S ; and the other type argument T is not a type
variable or wildcard; and neither | S | <: | T | nor | T | <: | S |.
• Each type argument is a type variable or wildcard, with upper bounds (from cap-
ture conversion, if necessary) of S and T ; and neither | S | <: | T | nor | T | <: | S |.
A type argument T 1 is said to contain another type argument T 2 , written T 2 <= T 1 , if the
set of types denoted by T 2 is provably a subset of the set of types denoted by T 1 under the
reflexive and transitive closure of the following rules (where <: denotes subtyping (§ 4.10 )):
? extends T <= ? extends S if T <: S
? super T <= ? super S if S <: T
T <= T
T <= ? extends T
T <= ? super T
The relationship of wildcards to established type theory is an interesting one,
which we briefly allude to here. Wildcards are a restricted form of existential
Search WWH ::




Custom Search