Friday, 19 June 2015

Class < ? > vs Class usage

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

Since Java 1.5 you should use the generic form wherever possible. Class<?> clearly states that you mean "an unknown class".

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.

No comments:

Post a Comment