【发布时间】:2018-09-23 20:28:09
【问题描述】:
我读到了原始 Java 类型(boolean、byte、char、short、int、long、float 和 double)并且关键字void 也表示为类Class 对象。那么这意味着int 是类Class 的对象那么为什么下面的语句不会抛出错误,因为.class 只与类名一起使用?
Class c = int.class
【问题讨论】:
-
"[...]
intis an object of class CLASS [...]" - 否。Class返回的实例int.class是Class的一个实例,代表int的类型。基元不是对象,它们不是引用类型。 -
好吧,如果它既不是CLASS也不是OBJECT,那么“int”是什么?
-
阅读§4 of the JLS。所有类型都在那里定义。基元(
boolean、byte、char、short、int、long、float和double)是基元。 -
链接(JLS 的§4)说 int 是我知道的原始数据,所以我应该假设原始数据类型 .class 持有异常,因为它只能与类名和int 不是类名而是原始数据类型?
-
请看@Andrew 's answer。他引用了定义
Class文字的相应 JLS 部分。
标签: java class object types int