What does <?> Mean in Java?

Possible duplicate:
Java generics

Eclipse gives me warnings for using 'rawtypes', and one of them is to add <?> . For instance:

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

What does this mean <?> ?

+4
source share
6 answers

Class<?> Should be interpreted as Class of something, but something is not known or does not care.

This is the use of common Java types. Class in Java 5 or more is a parameterized type, so the compiler expects a type parameter. Class<String> will work in the specific context of your code, but again, in many cases you do not need the actual type parameter, so you can just use the Class<?> , Which tells the compiler that you know Class expects the type parameter, but you anyway, that this parameter.

+4
source

Raw types refer to the use of 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 information: http://www.javapractices.com/topic/TopicAction.do?Id=224

+1
source

A warning means that List expects an item type, but you did not specify it. Since the compiler cannot say that you made a mistake or forgot something, it gives a warning.

There are several solutions: if you really do not care what is on the list, use <?> . This means that "maybe anything, I don't care."

If you may care, but this is old legacy code and you do not want to fix it, tell Eclipse to ignore it with @SuppressWarnings("rawtypes") and / or @SuppressWarnings("unchecked") if you are casting.

The best solution is to determine the correct type and its use. In this way, the compiler can help you catch more typos at compile time.

+1
source

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

Read the Java Generics Tutorial .

0
source

The compiler complains because it represents a common wildcard, since there can be any reference type in the type specified between the brackets. Java prefers strong typing and issues a warning to convince you to specify a specific type as a type specifier.

For instance:

 Class<String> parameter = String.class; 
0
source

All Articles