Java:如果类字面量应用于基本类型,那么产生什么类型的对象? [英] Java: an object of what type is produced if the class literal is applied to the primitive type?

查看:141
本文介绍了Java:如果类字面量应用于基本类型,那么产生什么类型的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我所知道的,Java中有一个特性称为类文字,应用于一个类型名称,使JVM产生一个类型的对象类<<?> ,这是一个通用类。更具体地说,以下结构是正确的:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆