Java:如果类字面量应用于基本类型,那么产生什么类型的对象? [英] Java: an object of what type is produced if the class literal is applied to the primitive type?
问题描述
的对象类<<?>
,这是一个通用类。更具体地说,以下结构是正确的: Class< type> o = type.class;
其中类型
是任何类型的名称可供使用。
令我困惑的是两个事实的组合。第一,类文字也可以与原始类型一起使用,即像 int.class
这样的构造是完全有效的。第二,不允许使用原始类型来参数化泛型,这意味着像 Class< int>
鉴于上述所有情况,使用了类
类型参数在 Class<?> c = int.class;
语句?
谢谢!
它只是一个 Class< Integer>
。
Class< Integer> o = int.class;
即使你使用了基本类型的类表示,它仍然是一个 Class< Integer>
:
Class< Integer> o = Integer.TYPE;
From what I know, there is a feature in Java called a class literal, which, applied to a type name, causes JVM to produce an object of type Class<?>
, which is a generic class. More specifically, the following construct is correct:
Class<type> o = type.class;
where type
is a name of any type available for use.
What confuses me is a combination of two facts. The first, that the class literal may be used with the primitive types as well, i.e. a construct like int.class
is perfectly valid. And the second, that one's not allowed to use primitive types to parameterize generics, which means that a construct like Class<int> c;
won't compile.
Given all the above, what type is used as the Class
type parameter in the Class<?> c = int.class;
statement?
Thanks!
It's simply a Class<Integer>
.
Class<Integer> o = int.class;
Even if you used the primitive type Class representation, it's still a Class<Integer>
:
Class<Integer> o = Integer.TYPE;
这篇关于Java:如果类字面量应用于基本类型,那么产生什么类型的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!