What does <?> stand for in Java? [duplicate]
Asked Answered
D

6

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?

Declare answered 27/6, 2011 at 14:35 Comment(2)
If you want go deeper with generics, see effective chapter 5Unseal
IMHO, It stands for confusion. ;)Geology
G
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

Gyral answered 27/6, 2011 at 14:38 Comment(1)
@Jason what version of the answer are you looking at?Gyral
R
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.

Respective answered 27/6, 2011 at 14:40 Comment(0)
G
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

Gyral answered 27/6, 2011 at 14:38 Comment(1)
@Jason what version of the answer are you looking at?Gyral
S
1

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

Solarize answered 27/6, 2011 at 14:39 Comment(0)
J
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.

Jenks answered 27/6, 2011 at 14:46 Comment(0)
H
0

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

Read the Java tutorial about generics.

Holston answered 27/6, 2011 at 14:40 Comment(0)
R
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;
Rhapsodize answered 27/6, 2011 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.