Difference between Bounded Type parameter (T extends) and Upper Bound Wildcard (? extends)

后端 未结 5 1078
余生分开走
余生分开走 2020-12-16 14:18

I know that there was a similar question already posted, although I think mine is somewhat different...

Suppose you have two methods:

// Bounded type         


        
5条回答
  •  佛祖请我去吃肉
    2020-12-16 14:55

    There are several differences between the two syntaxes during compile time :

    • With the first syntax, you can add elements to someList but with the second, you can't. This is commonly known as PECS and less commonly known as the PUT and GET prinicple.
    • With the first syntax, you have a handle to the type parameter T so you can use it to do things such as define local variables within the method of type T, cast a reference to the type T, call methods that are available in the class represented by T, etc. But with the second syntax, you don't have a handle to the type so you can't do any of this.
    • The first method can actually be called from the second method to capture the wildcard. This is the most common way to capture a wildcard via a helper method.

      private static  void processList(List someList) {
          T n = someList.get(0);
          someList.add(1,n); //addition allowed.   
      }
      
      private static void processList2(List someList) {
          Number n = someList.get(0);
          //someList.add(1,n);//Compilation error. Addition not allowed.
          processList(someList);//Helper method for capturing the wildcard
      }
      

    Note that since generics are compile time sugar, these differences at a broader level are only limited to the compilation.

提交回复
热议问题