What is the difference between List and List<? extends Number>?

后端 未结 3 2139
野的像风
野的像风 2020-12-03 15:28

I have a very basic question ragarding Java generics . I thought that both List and List are

3条回答
  •  一向
    一向 (楼主)
    2020-12-03 15:59

    Generics are compile time language features, means, they don't exist in run-time. In generic mechanism, for compile-time checks, they are not homogeneous, i.e. if you want to use polymorphism in generic type.

    Following gives you a compile time error, although it seems a valid definition:

     List list = new ArrayList ();
    

    whereas

     List list = new ArrayList ();
    

    is valid. Moreover, you can't use wildcard types on the right side:

     List list = new ArrayList ();
    

    won't be compiled.

提交回复
热议问题