Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Class Type

Tags:

java

generics

I have a block of code that works and I wanted to ask what exactly is happening here?

Class<?> normalFormClass = null;

---added---

The wildcard "<?>" is the part that is confusing for me. Why would this be used rather than not using it (generics)?

like image 978
pn1 dude Avatar asked Nov 14 '08 18:11

pn1 dude


People also ask

Is there a type class in Java?

Java provides the two types of inner classes are as follows: Local Classes or Method Local Inner Class. Anonymous Classes or Anonymous Inner Class.

How many class types are there in Java?

There are five kinds of classes: package-level , nested top-level , member , local , or anonymous . (The last four kinds are called inner classes . * A throw-away class that illustrates the five kinds of Java classes.


2 Answers

That means it can be a Class of any type. ? is a wildcard denoting the set of all types, or 'any'. So you can later do

Integer normalForm = new Integer();
normalFormClass = normalForm.getClass();

or

String normalForm = new String();
normalFormClass = normalForm.getClass();

If you are not aware of generics on Java, read http://java.sun.com/developer/technicalArticles/J2SE/generics/

As for the why, I think it might be to strictly express that you are using generics everywhere and your code is not compatible with older Java versions, or maybe to shut up some trigger happy IDEs. And yes,

Class foo 

and

Class<?> foo 

are equivalent.

like image 173
Vinko Vrsalovic Avatar answered Nov 16 '22 02:11

Vinko Vrsalovic


Also, the generic version

Class<?> normalClass = null;

is pretty much equivalent to the raw type version,

Class normalClass = null;

the main difference is that the latter will be compatible with versions of Java that do not support generics, like Java 1.4.

like image 21
John in MD Avatar answered Nov 16 '22 02:11

John in MD