Java - 如果int是一个基本数据类型,那么为什么存在int.class?

在使用Java反射时,我们可以发现可以通过创建构造函数对象来创建一个Class类对象的实例

Class c1 = Class.forName(className); 
Constructor cs = cl1.getConstructor(String.class,int.class);
Object obj = cs.newInstacne("小明",38);

为什么int作为一个基本数据类型却可以使用int.class呢?

其实int.class也是一个基本数据类型

int:

  • int.class is a primitive type.
  • int.class.isPrimitive() returns true.
  • int.class has no fields, public or private, and no methods.
  • an int is stored in memory as a literal numerical value. It can have no null value, because null is a memory pointer equal to 0, and 0 is a literal number.

Integer:

  • Integer.class is an object type.
  • Integer.class.isPrimitive() returns false.
  • Integer.class has methods as detailed in the class spec: Integer (Java Platform SE 7 )
  • An Integer is stored in memory as a reference (memory pointer) to an int primitive. It can be null, if it the pointer value is 0, the reserved location for null.

https://www.quora.com/In-Java-int-is-not-a-class-why-have-int-class-this-usage-What-is-the-difference-between-int-class-and-Integer-class

你可能感兴趣的:(Java - 如果int是一个基本数据类型,那么为什么存在int.class?)