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());
new 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 type
ArrayList<? 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:Bis a subtype ofAinstanceof Bis also aninstanceof ASince Java arrays are covariant:
B[]is a subtype ofA[]instanceof B[]is also aninstanceof A[]However, Java generics are invariant:
List<B>is NOT a subtype ofList<A>instanceof List<B>is NOT aninstanceof List<A>.When you have the following generic method declaration:
Then, as it’s explicitly stated here,
aandbmust both have the same type,List<T>, for some capture conversion of type parameter<T extends A>.Since
List<B>andList<C>are two different types, you can’t mix them as actual arguments fortest. Also, even thoughBandCare subtypes ofA, generics are invariant, so neitherList<B>norList<C>is aList<A>.Thus,
doesn’t compile, which is expected behavior.
See also
java.lang.ArrayStoreExceptionRelated questions
On generics typing rules:
List<Animal> animals = new ArrayList<Dog>()?Listis different fromList<Object>which is different from aList<?>On using
superandextends:Java Generics: What is PECS?extendsconsumersuper“superandextendsin Java Generics<E extends Number>and<Number>?List<? extends Number>data structures? (YOU CAN’T!)On actual generic bugs: