0

If there is any, what's the difference if I use Class or Class<?> as return type for example? Are there any cases where I should pay attention to use one or the other?

Steven
  • 1,218
  • 3
  • 18
  • 38
  • use `Class>`, `Class` is a [rawtype](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – zapl Dec 05 '13 at 16:12

2 Answers2

2
  1. Class is a rawtype and you should avoid using such.
  2. Class<?> represents a parametrized by an unknown type Class.

At runtime, there's no difference between Class and Class<?>, at all. First, they are both converted to a generic Class<Object> and finally the type is erased (because of the type erasure).

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • Why would I want to avoid rawtypes, if these two are converted to the same at runtime? – G_hi3 Oct 15 '15 at 11:45
  • 1
    Because raw types are compilation-error prone. If you use Generics, the compiler checks if the operations you do with the types are correct and rejects compiling if a possible `ClassCastException` could occur at Runtime. Using raw types only, the compiler shows a warning only, saying "I hope you know what you do". – Konstantin Yovkov Oct 15 '15 at 11:49
0

This is a subset of the more general generics question about the difference between Anything and Anything<?>.

There is a reasonably clear description of it in the main java tutorials on generics: http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html

Tim B
  • 40,716
  • 16
  • 83
  • 128