int.class 是什么意思 [英] What does int.class mean

查看:31
本文介绍了int.class 是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java中int、float等都是原始类型.包装类用于万一我们需要将它与泛型一起使用.但是下面的声明在java中仍然有效,

 ClassintClass=int.class

即使 int.class 是原始类型,我们如何调用它?

解决方案

一个原语变成一个对象

<块引用>

对于原语,在相应的包装类中有 Class 对象可用作名为 TYPE 的常量——即 int.class 更改为 java.lang.Integer.TYPE .对于其他类型,编译器在被编译的类中创建一个私有成员变量以保存 Class 对象,并生成代码以使用 Class.forName() 初始化该成员.

找到一些讨论

还有一个很好的这里的讨论和你的例子此链接中也有介绍.

从那里说几句话:

<块引用>

一个类怎么可能是原始的?让我们再混淆一下.我们可以通过编码访问代表定义类的 Class 对象,比如:

Equation.class//返回方程式类对象

但是,我们也可以说:

int.class

<块引用>

获得一个名为int"的类对象.请注意,我们没有将 getClass() 方法发送到对象;我们使用了内置原始类型 (int) 的保留字,并使用点表示法访问了它的类字段".这将返回一个 Class 对象!

In java int, float and etc., are primitive types. Wrapper classes are used in case we need to use it with generics. But still the following declaration works in java,

 Class<Integer> intClass=int.class

How can we call int.class even though it is a primitive type?

解决方案

A primitive becoming an Object

For primitives, there are Class objects available as constants named TYPE in the corresponding wrapper classes -- i.e. int.class is changed to java.lang.Integer.TYPE . For other types, the compiler creates a private member variable in the class being compiled to hold the Class object, and generates code to initialize that member using Class.forName() .

Found some discussion

And a nice discussion here and your example also covered in this link.

A few words from there :

how can a Class be a primitive? Let's confuse things a bit more. We can access the Class object representing a defined class by coding, say:

Equation.class // returns the Equation Class object

But, we can also say:

int.class

obtain a Class object whose name is "int". Note we have not sent the getClass() method to an object; we have used the reserved word for a built-in primitive type (int) and, using dot notation, accessed its class "field." And this returns a Class object!

这篇关于int.class 是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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