Java:如果将 class 文字应用于基本类型,会生成什么类型的对象?
Java: an object of what type is produced if the class literal is applied to the primitive type?
据我所知,Java 中有一个称为 class 文字的功能,它应用于类型名称,导致 JVM 生成类型为 Class<?>
的对象,这是一个通用的 class。更具体地说,以下构造是正确的:
Class<type> o = type.class;
其中 type
是可用的任何类型的名称。
让我困惑的是两个事实的结合。首先,class 文字也可以与原始类型一起使用,即像 int.class
这样的结构是完全有效的。第二,不允许使用原始类型来参数化泛型,这意味着像 Class<int> c;
这样的结构将无法编译。
鉴于以上所有内容,Class<?> c = int.class;
语句中的 Class
类型参数使用什么类型?
谢谢!
这只是一个Class<Integer>
。
Class<Integer> o = int.class;
即使你使用原始类型 Class 表示,它仍然是 Class<Integer>
:
Class<Integer> o = Integer.TYPE;
据我所知,Java 中有一个称为 class 文字的功能,它应用于类型名称,导致 JVM 生成类型为 Class<?>
的对象,这是一个通用的 class。更具体地说,以下构造是正确的:
Class<type> o = type.class;
其中 type
是可用的任何类型的名称。
让我困惑的是两个事实的结合。首先,class 文字也可以与原始类型一起使用,即像 int.class
这样的结构是完全有效的。第二,不允许使用原始类型来参数化泛型,这意味着像 Class<int> c;
这样的结构将无法编译。
鉴于以上所有内容,Class<?> c = int.class;
语句中的 Class
类型参数使用什么类型?
谢谢!
这只是一个Class<Integer>
。
Class<Integer> o = int.class;
即使你使用原始类型 Class 表示,它仍然是 Class<Integer>
:
Class<Integer> o = Integer.TYPE;