Java class with concrete type as parameter

前端 未结 2 946
长发绾君心
长发绾君心 2020-12-21 13:33

Is there any point in declaring a class with \"concrete\" types as generics?

If yes, what\'s the use for it?

If no, any specific reason why the compiler is

2条回答
  •  醉酒成梦
    2020-12-21 14:08

    In the class definition the parameter you called Integer could be also just be T without changing the sense.

    AFIK you can omit the generic just in Java 7 where the compiler adds this automatically it is anyway not stored at runtime. So you must define the generic in a lefthand definition, the only exception of this is using the question mark which is used as a wildcard.

    // here is the generic missing the compiler cannot guess it:
    SomeClass<> iSome = new SomeClass<>();
    // here does the compiler know that you want a Double
    SomeClass jSome = new SomeClass<>();
    // this will also work
    SomeClass kSome = new SomeClass();
    

提交回复
热议问题