繁体   English   中英

int.class的目的和功能是什么

[英]What's the purpose and function of int.class

我意外地发现这有效:

Class<?> a;

a = int.class;
System.out.println(a); // int

a = Integer.class;      
System.out.println(a); // class java.lang.Integer

对于原语来说,它真正意味着什么?

我试过, List<int>List<int.class> (是的,我知道我必须使用Integer)。 另外,显然,我无法在基元上调用getClass() ,因此它对任何类型的类型检查都没用。

在什么情况下我会使用int.class ,为什么它甚至出现在语言中?

当您尝试通过反射API查找具有int参数的方法时,可以使用它。

从规格:

The type of p.class, where p is the name of a primitive type 
(§4.2), is Class<B>, where B is the type of an expression of 
type p after boxing conversion (§5.1.7).

A class literal evaluates to the Class object for the named 
type (or for void) as defined by the defining class loader 
(§12.2) of the class of the current instance.

那怎么样

Class<Integer> a = int.class;

何时使用:大量反射用例(例如在argparse4j中,它们使用它从命令行参数映射到方法)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM