Java class with concrete type as parameter

前端 未结 2 941
长发绾君心
长发绾君心 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<Double> jSome = new SomeClass<>();
    // this will also work
    SomeClass<?> kSome = new SomeClass<Boolean>();
    
    0 讨论(0)
  • 2020-12-21 14:17

    It's not what you think. You're creating a generic type parameter called Integer which shadows java.lang.Integer.

    0 讨论(0)
提交回复
热议问题