Java Generics Capture List<?>

前端 未结 3 1748
孤独总比滥情好
孤独总比滥情好 2021-01-13 11:48

I was looking at the Java Generics documentation and found this piece of code,

public class WildcardError {

void foo(List l) {
    //This give a co         


        
3条回答
  •  情深已故
    2021-01-13 12:17

    The compiler reports an error because there is no way -- in general -- that it can tell whether two expressions, (in this case l and l) refer to the same list.

    Related, somewhat generalized, question:

    • How does the JLS specify that wildcards cannot be formally used within methods?

提交回复
热议问题