java反射中的基本数据类型

反射的过程中我们有的时间可能需要通过入参来获的方法。对于基础数据类型我们发现使用其包装类的class是无法匹配的。也就是说自动装包拆包的功能不适用于反射。

原始类型对应的虚拟机中的class实例和封装类对应的class实例是不相同的。
如:
int 对应的class实例为 int.class 或者 Integer.TYPE,但是 Integer 对应的 class 实例为 Integer.class
因此,你的代码中要通过反射执行 setIntField(int)时,需要通过以下方式获取 method:
Method method= cls.getMethod("setIntField", int.class);
2.boolean是基本型,所以boolean.class.isPrimitive==true
Boolean.class.isPrimitive==false
boolean.class==Boolean.TYPE

发布了29 篇原创文章 · 获赞 11 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/yue_hu/article/details/80647921