I understand what wrappers are, but from the documentation you seem to be able to get an instance of a Class object of type int. Does this return the wrapper or some sort of Class instance of int? Because it wouldn't make sense if it did, due to generics and type erasure. Isn't it true that you can only get Class instances of actual classes not primitives? When they say represent do they mean wrapper of something else?
Here is what the JavaDoc says, and why I'm confused
TYPE
public static final Class TYPE
The Class instance representing the primitive type int.
Since:
JDK1.1
The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int .
int. class is same type as Class<Integer> as per the specifications. From Docs: The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.
The data type of the return value must match the method's declared return type; you can't return an integer value from a method declared to return a boolean.
It returns the string representation which is the string “class” or “interface”, followed by a space, and then by the fully qualified name of the class. If the Class object represents a primitive type, then this method returns the name of the primitive type and if it represents void then it returns “void”.
Isn't it true that you can only get Class instances of actual classes not primitives?
Sort of.
But sometimes you need to have some meta-data for "primitive integer". For example when looking at the method signature. Then you get a Class[]
for the parameters, and you somehow need to differentiate between public void test(Integer x)
and public void test(int x)
.
So to facilitate this, there are special Class instances for the primitive types.
To take this a step further, there is even java.lang.Void.TYPE:
Class<Void> x = void.class;
Insightful Javadoc:
/**
* Determines if the specified <code>Class</code> object represents a
* primitive type.
*
* <p> There are nine predefined <code>Class</code> objects to represent
* the eight primitive types and void. These are created by the Java
* Virtual Machine, and have the same names as the primitive types that
* they represent, namely <code>boolean</code>, <code>byte</code>,
* <code>char</code>, <code>short</code>, <code>int</code>,
* <code>long</code>, <code>float</code>, and <code>double</code>.
*
* <p> These objects may only be accessed via the following public static
* final variables, and are the only <code>Class</code> objects for which
* this method returns <code>true</code>.
*
* @return true if and only if this class represents a primitive type
*
* @see java.lang.Boolean#TYPE
* @see java.lang.Character#TYPE
* @see java.lang.Byte#TYPE
* @see java.lang.Short#TYPE
* @see java.lang.Integer#TYPE
* @see java.lang.Long#TYPE
* @see java.lang.Float#TYPE
* @see java.lang.Double#TYPE
* @see java.lang.Void#TYPE
* @since JDK1.1
*/
public native boolean isPrimitive();
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