Java multiple generic collection parameters compile error
So strange! Please have a look the code first:
public class A {}
public class B extends A {}
public class C extends A {}
public class TestMain {
public <T extends A> void test(T a, T b) {}
public <T extends A> void test(List<T> a, List<T> b) {}
public void test1(List<? extends A> a, List<? extends A> b) {}
public static void main(String[] args) {
new TestMain().test(new B(), new C());
n开发者_如何学Goew TestMain().test(new ArrayList<C>(), new ArrayList<C>());
new TestMain().test(new ArrayList<B>(), new ArrayList<C>());
new TestMain().test1(new ArrayList<B>(), new ArrayList<C>());
}
}
The statement new TestMain().test(new ArrayList<B>(), new ArrayList<C>())
gets a compilation error:
Bound mismatch: The generic method test(T, T) of type TestMain is not applicable for the arguments
(ArrayList<B>, ArrayList<C>)
. The inferred typeArrayList<? extends A>
is not a valid substitute for the bounded parameter<T extends A>
However:
new TestMain().test(new B(), new C()) --> compiled ok
new TestMain().test(new ArrayList<C>(), new ArrayList<C>()) --> compiled ok
new TestMain().test1(new ArrayList<B>(), new ArrayList<C>()) --> compiled ok
If we define the generic before the method name, it seems the type of the second generic List
parameter must be the same as that of the first. But there is no restriction if we define generic in parameters.
Is it a feature or a bug of the compile program? Is there some documentation about it?
There is absolutely no bug; you simply misunderstood the subtyping rules in generics.
Since we have B extends A
:
B
is a subtype ofA
- an
instanceof B
is also aninstanceof A
Since Java arrays are covariant:
B[]
is a subtype ofA[]
- an
instanceof B[]
is also aninstanceof A[]
However, Java generics are invariant:
List<B>
is NOT a subtype ofList<A>
- a
instanceof List<B>
is NOT aninstanceof List<A>
.
When you have the following generic method declaration:
public <T extends A> void test(List<T> a, List<T> b)
Then, as it's explicitly stated here, a
and b
must both have the same type, List<T>
, for some capture conversion of type parameter <T extends A>
.
Since List<B>
and List<C>
are two different types, you can't mix them as actual arguments for test
. Also, even though B
and C
are subtypes of A
, generics are invariant, so neither List<B>
nor List<C>
is a List<A>
.
Thus,
test(new ArrayList<B>(), new ArrayList<C>()); // error!!! doesn't compile!!!
doesn't compile, which is expected behavior.
See also
- Java Tutorials/Generics
- Subtyping
- More fun with wildcards
- JLS 10.10 Array Store Exception and
java.lang.ArrayStoreException
- Java array subtyping, being covariant, is not typesafe; generics, being invariant, is.
Related questions
On generics typing rules:
- Any simple way to explain why I cannot do
List<Animal> animals = new ArrayList<Dog>()
? - java generics (not) covariance
- What is a raw type and why shouldn’t we use it?
- Explains how raw type
List
is different fromList<Object>
which is different from aList<?>
- Explains how raw type
On using super
and extends
:
Java Generics: What is PECS?
- From Effective Java 2nd Edition: "producer
extends
consumersuper
"
- From Effective Java 2nd Edition: "producer
- What is the difference between
super
andextends
in Java Generics - What is the difference between
<E extends Number>
and<Number>
? - How can I add to
List<? extends Number>
data structures? (YOU CAN'T!)
On actual generic bugs:
- Generics compiles and runs in Eclipse, but doesn’t compile in javac
It's not a bug, it's just that generics are complex. Try changing your second test method to:
public <T extends A, K extends A> void test(List<T> a, List<K> b) {
Basically in what you've got there is no type T that can satisfy what you're passing in, unlike the first method where B and C are just treated as A. The actual name for this behaviour escapes me but there should be plenty of examples in the literature.
In short, even though B is a child of A, List<B> is not a child of List<A>.
精彩评论