Java: an object of what type is produced if the cl

2019-08-02 19:06发布

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!

1条回答
放荡不羁爱自由
2楼-- · 2019-08-02 19:41

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;
查看更多
登录 后发表回答