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)?
Java provides the two types of inner classes are as follows: Local Classes or Method Local Inner Class. Anonymous Classes or Anonymous Inner Class.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With