Difference between Class and Class<?>

后端 未结 4 2058
长情又很酷
长情又很酷 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:40

    Class<?> shows that you're intentionally writing Java 5-level code that doesn't know or care what class you are dealing with. Leaving out the makes it look like old code or code written by someone who hasn't learned generics yet.

    0 讨论(0)
  • 2021-02-01 13:48

    "it look like old code or code written by someone who hasn't learned generics yet." This is an correct statement. Class (pronounced "class of unknown"), that is, a class whose type matches anything. It's called a wild-card type for obvious reasons.

    for example:

    public void drawAll(List<Shape> shapes) {
        for (Shape s: shapes) {
            s.draw(this);
       }
    }
    

    type rules say that drawAll() can only be called on lists of exactly Shape: it cannot, for instance, be called on, say, a List<Circle>. That is unfortunate, since all the method does is read shapes from the list, so it could just as well be called on a List<Circle>.

    What we really want is for the method to accept a list of any kind of shape.

    public void drawAll (List <? extends Shape> shapes) {
    
    }
    

    read more: http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html

    0 讨论(0)
  • 2021-02-01 13:50

    Class javadoc:

    Type Parameters: T - the type of the class modeled by this Class object. For example, the type of String.class is Class<String>. Use Class<?> if the class being modeled is unknown.

    Use of Class without the type parameter is similar to using any generic class (Map, List, etc.) without the type parameter - either it's a pre-1.5 legacy usage or it's just a segment of code that does not care about unchecked type casting.

    0 讨论(0)
  • 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<String>  // 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<String> 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.

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