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,465
  • 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
  • 178,603
  • 161
  • 580
  • 939
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,196
  • 6
  • 35
  • 60
Naftali
  • 142,114
  • 39
  • 237
  • 299
1

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

Sean Patrick Floyd
  • 284,665
  • 62
  • 456
  • 576
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
  • 310,263
  • 103
  • 579
  • 794
0

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

Read the Java tutorial about generics.

JB Nizet
  • 657,433
  • 87
  • 1,179
  • 1,226
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
  • 8,722
  • 3
  • 24
  • 33