java generics bounds type

前端 未结 3 494
说谎
说谎 2021-01-11 17:40

Are following two signatures same?

public static  void work(Class type, T instance);

and

public stati         


        
3条回答
  •  误落风尘
    2021-01-11 18:13

    For any set of arguments, if a valid choice of T exists for

    void work(Class type, T instance)

    then a valid choice of T and S exists for

    void work(Class type, S instance)

    and vice versa.

    So from a theoretical point of view, they are equivalent. However, due to limited inference, it may be the case that, when type arguments are not given, one will compile while the other doesn't for certain arguments. In such a case, it is always possible to explicitly specify a valid set of type arguments for the case that doesn't compile, to make it compile.

    Since they are equivalent, an API should always prefer the simpler form, i.e. without the S.

提交回复
热议问题