Strange wildcard behavior when calling a method

I went through a wildcard section in Java where I am stuck with this code below

static <T> void type(List<? super T> list){ //... } static <T> void type2(List<? extends T> list){ //... } 

If you call these methods with this code, it gives an error and clear

 List<?> unbounded=new ArrayList<Long>(); //! type(unbounded); //:Error here //! type2(unbounded); //:Same Error here 

But if you change the signature of these methods by adding one additional arg argument, like this

  static <T> void type(List<? super T> list, T arg){ //... } static <T> void type2(List<? extends T> list, T arg){ //... } 

and name them

 List<?> unbounded=new ArrayList<Long>(); Long lng=90L; //! type(unbounded,lng); //:Error here type2(unbounded,lng); //No error in this one now 

Why does this behavior just add one extra argument? How does a restricted list accept unlimited simply by adding one xtra arg?

+5
source share

Source: https://habr.com/ru/post/1275689/


All Articles