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?

2
  • If you want go deeper with generics, see effective chapter 5 Commented Jun 27, 2011 at 14:41
  • IMHO, It stands for confusion. ;) Commented Jun 27, 2011 at 14:41

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

@Jason what version of the answer are you looking at?
1

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

Comments

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.

Comments

0

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

Read the Java tutorial about generics.

Comments

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;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.