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

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

问题描述

在java int,float等中,是原始类型。我们需要使用包装类来使用泛型。
但是以下声明仍适用于java,

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

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

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

推荐答案

原语成为对象

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

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() .

找到一些讨论

还不错这里讨论,您的示例也在此链接中介绍。

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

从那里说几句:


一个Class如何成为一个原语?让我们把事情搞得一团糟。我们可以通过编码访问表示已定义类的Class对象,例如:

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

但是,我们也可以说:

int.class




获取名为int的Class对象。注意我们没有将getClass()方法发送给对象;我们使用保留字作为内置基元类型(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天全站免登陆