Difference between Class and Class<?>

后端 未结 4 2093
长情又很酷
长情又很酷 2021-02-01 13:10

What is the difference between a Class and a Class declaration.

  • Class a;
  • Class b;
4条回答
  •  终归单人心
    2021-02-01 13:53

    It's the same as with all generic and raw types:

    Class          // An unknown class (raw type)
    Class       // An unknown class (generic version)
    Class  // The String class
    

    In this special case there's no much practical difference between Class and Class because they both denote an unknown class. Depending on the existing declarations the compiler can demand a generic type instead of a raw type.

    But: Since Java 1.5 you should use the generic form wherever possible. Class clearly states that you mean "an unknown class", Class cleary states that you mean the String class. A raw Class could mean both.

    In the end it makes not much of a difference to the compiler but it makes a huge difference in making the intentions of your code more understandable and maintainable.

提交回复
热议问题