Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Class clazz and Class<?> clazz in java?

I need to make use of reflection in java. I understand that Class clazz creates a variable representing a Class object. However, I am trying to reference a Class object from a String using the forName("aClassName") method. My IDE (Eclipse), seems to prefer the notation Class<?> clazz for declaring the variable. I have seen this notation many times elsewhere. What does this mean?

Edit: Removed reference to ternary operator as it is not relevant to this question.

like image 205
Andrew Harasta Avatar asked Feb 28 '13 20:02

Andrew Harasta


2 Answers

Class is a raw type - it's basically a generic type that you're treating as if you didn't know about generics at all.

Class<?> is a generic type using an unbound wildcard - it basically means "Class<Foo> for some type Foo, but I don't know what".

Similarly you can have wildcards with bounds:

  • Class<? extends InputStream> means "Class<Foo> for some type Foo, but I don't know what so long as it's InputStream or a subclass"

  • Class<? super InputStream> means "Class<Foo> for some type Foo, but I don't know what so long as it's InputStream or a superclass"

See also the Java Generics FAQ for a lot more information:

  • Raw types
  • Wildcards

And the Java Language Specification:

  • Raw types (section 4.8)
  • Type arguments and wildcards (section 4.5.1)

In particular, from the raw types section:

Raw types are closely related to wildcards. Both are based on existential types. Raw types can be thought of as wildcards whose type rules are deliberately unsound, to accommodate interaction with legacy code. Historically, raw types preceded wildcards; they were first introduced in GJ, and described in the paper Making the future safe for the past: Adding Genericity to the Java Programming Language by Gilad Bracha, Martin Odersky, David Stoutamire, and Philip Wadler, in Proceedings of the ACM Conference on Object-Oriented Programming, Systems, Languages and Applications (OOPSLA 98), October 1998.

like image 95
Jon Skeet Avatar answered Sep 20 '22 10:09

Jon Skeet


The first thing to realize is that, in this case, the "?" is NOT the ternary operator, but is part of Java's generics implementation and indicates that the type of Class is unspecified, as some of the other answers have already explained.

To clarify the question about the ternary operator, it is actually very simple.

Imagine you have the following if statement:

boolean correct = true;
String message;

if (correct) {
  message = "You are correct.";
} else {
  message = "You are wrong.";
}

You can rewrite that with the ternary operator (think of it as the if-else-shortcut operator):

message = (correct) ? "You are correct." : "You are wrong.";

However, it's best to avoid the ternary operator for all but the simplest statements in order to improve the readability of your code.

like image 24
yogaphil Avatar answered Sep 23 '22 10:09

yogaphil