Java int.class返回什么
我理解包装器是什么,但从文档中,您似乎能够获得int类型的类对象的实例。这是否返回包装器或某种int类型的类实例?由于泛型和类型擦除,如果它这样做了就没有意义了。难道你不能只得到实际类的类实例而不是原语吗?当他们说代表时,他们是指其他东西的包装吗 以下是JavaDoc所说的,以及我感到困惑的原因Java int.class返回什么,java,reflection,Java,Reflection,我理解包装器是什么,但从文档中,您似乎能够获得int类型的类对象的实例。这是否返回包装器或某种int类型的类实例?由于泛型和类型擦除,如果它这样做了就没有意义了。难道你不能只得到实际类的类实例而不是原语吗?当他们说代表时,他们是指其他东西的包装吗 以下是JavaDoc所说的,以及我感到困惑的原因 TYPE public static final Class TYPE The Class instance representing the primitive type int.
TYPE
public static final Class TYPE
The Class instance representing the primitive type int.
Since:
JDK1.1
难道你不能只得到实际类的类实例而不是原语吗
有点
但有时您需要一些“原始整数”的元数据。例如,当查看方法签名时。然后,您将得到一个参数的类[]
,您需要以某种方式区分公共无效测试(整数x)
和公共无效测试(int x)
因此,为了便于实现这一点,基本类型有一些特殊的类实例
为了更进一步,还有:
我没有遵循“由于泛型和类型擦除……您不是只能得到实际类的
Class
实例而不是原语”的逻辑吗泛型和类型擦除与类和原语之间的差异无关。您可能会喜欢void.class
:-)有时还需要注意这些特殊的类类型。我曾经写过一些代码,在那里我调用了.getSuperclass(),我认为我会很好,因为所有东西都来自一个对象,对吗?没有。我发现有趣的是,Integer.TYPE
被声明为类而不是类
——似乎有点错误。
Class<Void> x = void.class;
/**
* 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();