java 反射的一些困惑

今天学习java反射,碰到一个小问题一直想不明白。

就是Integer.class和Integer.TYPE有什么区别?

这两者都是取得Integer的字节码吗?

应该都是取Class的引用的,但是取得是不同的Class的引用。

Interger.TYPE == int.class 是基本类型int的Class的引用,int是基本类型在虚拟机运行时就已经加载了他的Class.

Interger.class 是int的封装类Interger的引用。 因此二者是不同的。

static Class TYPE
The Class object that represents the primitive type int.

从java 文档中可以看出,他代表的就是Integer字节码, 而Integer.class 就更不用说了

jdk文档上的

TYPE
public static final Class TYPE 表示基本类型 int 的 Class 实例。

输出
System.out.println(Integer.class); class java.lang.Integer
System.out.println(Integer.TYPE); int

下面是Integer.TYPE的源码
public static final Class TYPE = (Class) Class.getPrimitiveClass("int");
表示基本类型 int 的 Class 实例,这里注意是基本类型,这样应该能懂了吧

我们菜鸟水平,一般不会用到。 class字节码是在反射技术中用到的,通常是架构师的级别。如果你感兴趣,可以去看下java 反射技术

一个是取字节码 一个取类型。。。不可以同日而语

取的是基本类型int的Class不是Integer的

在指定基本类型时,要使用对应的包类并使用.TYPE.
比如,指定int类型时,使用Integer.TYPE
指定Integer类型时,使用Integer.class.