我理解包装器是什么,但是从文档中你似乎能够获得int类型的Class对象的实例。这会返回包装器或某种类的int实例吗?因为泛型和类型擦除所以它没有意义。难道你只能获得实际类的Class实例而不是原语吗?当他们说代表他们意味着其他东西的包装?
以下是JavaDoc所说的内容,以及为什么我感到困惑
TYPE
public static final Class TYPE
The Class instance representing the primitive type int.
Since:
JDK1.1
答案 0 :(得分:7)
您是否只能获得实际类的Class实例而不是原语?
排序。
但有时你需要为“原始整数”提供一些元数据。例如,在查看方法签名时。然后,您获得参数的Class[]
,并且您需要区分public void test(Integer x)
和public void test(int x)
。
为了方便这一点,原始类型有特殊的类实例。
为了更进一步,甚至有java.lang.Void.TYPE:
Class<Void> x = void.class;
富有洞察力的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();