178

My question is as above. Sorry, it's probably a duplicate but I couldn't find an example with the <?> on the end.

Why would you not just use Class as the parameter?

0xCursor
  • 2,242
  • 4
  • 15
  • 33
David
  • 19,577
  • 28
  • 108
  • 128

7 Answers7

155

Class is a parameterizable class, hence you can use the syntax Class<T> where T is a type. By writing Class<?>, you're declaring a Class object which can be of any type (? is a wildcard). The Class type is a type that contains meta-information about a class.

It's always good practice to refer to a generic type by specifying his specific type, by using Class<?> you're respecting this practice (you're aware of Class to be parameterizable) but you're not restricting your parameter to have a specific type.

Reference about Generics and Wildcards: http://docs.oracle.com/javase/tutorial/java/generics/wildcards.html

Reference about Class object and reflection (the feature of Java language used to introspect itself): https://www.oracle.com/technetwork/articles/java/javareflection-1536171.html

Fifi
  • 467
  • 6
  • 17
manub
  • 3,990
  • 2
  • 24
  • 33
  • 13
    What's the benefit of doing this over just simply using `Class` without a type? They seem to represent the same thing. – ashes999 Jun 22 '13 at 14:47
  • 1
    I thought the whole point of a generic is you don't know the class type upfront. Otherwise you would just define a function to use a particular class type for the parameter. This question mark still doesn't make sense. – Brain2000 Mar 06 '15 at 17:48
  • 8
    There is no other benefit except you're telling the compiler that "I know this is a generic class, but I don't know or care about the actual type, so instead of giving a concrete type I'm giving the `?` wildcard.". If you don't give the wildcard, the compiler assumes that you either forgot the type or didn't know the class was generic and will warn you about it. – Kayaman Feb 29 '16 at 07:51
  • 1
    What's the Kotlin equivalent of `Class>` – Jack Guo Jun 14 '18 at 17:50
  • 1
    simply ``Class`` cannot be pass to a argument which type is ``Class>``, So ``Class>`` is more convenient for all situation. – petertc Oct 05 '18 at 07:08
  • @JGuo in Kotlin you have to use either `out` or `in` modifier. – Egis Sep 02 '19 at 13:36
100

This <?> is a beast. It often leads to confusion and errors, because, when you see it first, then you start believing, <?> is a wildcard for any java type. Which is .. not true. <?> is the unknown type, a slight and nasty difference.

It's not a problem when you use it with Class. Both lines work and compile:

Class anyType = String.class;
Class <?> theUnknownType = String.class;

But - if we start using it with collections, then we see strange compiletime errors:

List<?> list = new ArrayList<Object>();  // ArrayList<?> is not allowed
list.add("a String");                    // doesn't compile ...

Our List<?> is not a collection, that is suitable for just any type of object. It can only store one type: the mystic "unkown type". Which is not a real type, for sure.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
10

It means your Class reference can hold a reference to any Class object.

It's basically the same as "Class" but you're showing other people who read your code that you didn't forget about generics, you just want a reference that can hold any Class object.

Bruce Eckel, Thinking in Java:

In Java SE5, Class<?> is preferred over plain Class, even though they are equivalent and the plain Class, as you saw, doesn’t produce a compiler warning. The benefit of Class<?> is that it indicates that you aren’t just using a non-specific class reference by accident, or out of ignorance. You chose the non-specific version.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
Filip
  • 1,451
  • 1
  • 11
  • 19
9

It's a generics literal. It means that you don't know the type of class this Class instance is representing, but you are still using the generic version.

  • if you knew the class, you'd use Class<Foo>. That way you can create a new instance, for example, without casting: Foo foo = clazz.newInstance();
  • if you don't use generics at all, you'll get a warning at least (and not using generics is generally discouraged as it may lead to hard-to-detect side effects)
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
5

In generics, an unknown type is represented by the wildcard character "?". Read here for official example.

Jasonw
  • 5,054
  • 7
  • 43
  • 48
3

That means a Class with a type of anything (unknown).

You should read java generics tutorial to get to understand it better

Matt
  • 74,352
  • 26
  • 153
  • 180
fmucar
  • 14,361
  • 2
  • 45
  • 50
2

It means, the Class reference type can hold any Class object which represents any type. If JVM loads a type, a class object representing that type will be present in JVM. we can get the metadata regarding the type from that class object which is used very much in reflection package.

Suppose you have a a class named "myPackage.MyClass". Assuming that is in classpath, the following statements are equivalent.

Class<?> myClassObject = MyClass.class; //compile time check

Class<?> myClassObject = Class.forname("myPackage.MyClass"); //only runtime check

This works in a similar fashion if the Class<?> reference is in method argument as well.

Please note that the class "Class" does not have a public constructor. So you cannot instantiate "Class" instances with "new" operator.

SRaj
  • 1,168
  • 1
  • 14
  • 36