Java multiple generic collection parameters compile error?

There is absolutely no bug; you simply misunderstood the subtyping rules in generics.

There is absolutely no bug; you simply misunderstood the subtyping rules in generics. Since we have B extends A: B is a subtype of A an instanceof B is also an instanceof A Since Java arrays are covariant: B is a subtype of A an instanceof B is also an instanceof A However, Java generics are invariant: List is NOT a subtype of List a instanceof List is NOT an instanceof List. When you have the following generic method declaration: public void test(List a, List b) Then, as it's explicitly stated here, a and be must both have the same type, List, for some capture conversion of type parameter .

Since List and List 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 nor List is a List. Thus, test(new ArrayList(), new ArrayList()); // 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 animals = new ArrayList()? Java generics (not) covariance What is a raw type and why shouldn’t we use it?

Explains how raw type List is different from List which is different from a List On using super and extends: Java Generics: What is PECS? From Effective Java 2nd Edition: "producer extends consumer super" What is the difference between super and extends in Java Generics What is the difference between and? How can I add to List data structures?(YOU CAN'T!

) On actual generic bugs: Generics compiles and runs in Eclipse, but doesn’t compile in javac.

3 Of course, co- and contra- variance, thanks PGL. – CurtainDog May 24 '10 at 4:30.

It's not a bug, it's just that generics are complex. Try changing your second test method to: public void test(List a, List 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 is not a child of List.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions