5

Possible Duplicate:
Java Generics

In Eclipse, I am given warnings for using 'rawtypes' and one of it's fixes is to add <?>. For example:

    Class parameter = String.class;
    //Eclipse would suggest a fix by converting to the following:
    Class<?> parameter = String.class;

What does this <?> actually mean?

Community
  • 1
  • 1
trigoman
  • 3,585
  • 2
  • 21
  • 20

6 Answers6

5

Class<?> should be interpreted as a Class of something, but the something isn't known or cared about.

It's the use of Java generic types. Class in Java 5 or greater is a parameterized type, so the compiler expects a type parameter. Class<String> would work in the specific context of your code, but again, in many cases you don't care about the actual type parameter, so you can just use Class<?> which is telling the compiler that you know Class expects a type parameter, but you don't care what the parameter is.

Jason S
  • 184,598
  • 164
  • 608
  • 970
2

Raw types refer to using a generic type without specifying a type parameter. For example, List is a raw type, while List<String> is a parameterized type

See this document for more info: http://www.javapractices.com/topic/TopicAction.do?Id=224

josefx
  • 15,506
  • 6
  • 38
  • 63
Naftali
  • 144,921
  • 39
  • 244
  • 303
1

This is way beyond the scope of this site, but here are some links:

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
1

The warning means that List expects the type of the items but you didn't specify one. Since the compiler can't tell whether you made a mistake or forgot something, it gives the warning.

There are several solutions: If you really don't care what's in the list, use <?>. It means "can be anything, I don't care."

If you probably care but this is old legacy code and you don't want to fix it, tell Eclipse to ignore it with the @SuppressWarnings("rawtypes") and/or @SuppressWarnings("unchecked") if you're casting.

The best solution is to figure out the correct type and use that. This way, the compiler can help you catch more typos at compile time.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

Class is a generic type since Java 5 (years ago).

Read the Java tutorial about generics.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

The compiler is complaining because represents a generic wildcard, as in the type specified between the brackets could be any reference type. Java prefers strong typing and is issuing you a warning to convince you to specify a concrete type as the type specifier.

For example:

Class<String> parameter = String.class;
Chris Hutchinson
  • 9,082
  • 3
  • 27
  • 33