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?
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.
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
This is way beyond the scope of this site, but here are some links:
- Angelika Langer's Generics FAQ
- Sun Java Tutorial / Generics
- Java Generics and Collections (Book)
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.
Class is a generic type since Java 5 (years ago).
Read the Java tutorial about generics.
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;
精彩评论