[英]What does int.class return
我理解包裝器是什么,但從文檔中你似乎能夠獲得int類型的Class對象的實例。 這會返回包裝器或某種類的int實例嗎? 因為泛型和類型擦除所以它沒有意義。 難道你只能獲得實際類的Class實例而不是原語嗎? 當他們說代表他們意味着其他東西的包裝?
這是JavaDoc所說的內容,以及為什么我感到困惑
TYPE
public static final Class TYPE
The Class instance representing the primitive type int.
Since:
JDK1.1
難道你只能獲得實際類的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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.