Java中基本类型与包装类的Class对象内存隔离,即int.class≠Integer.class,因前者是JVM预置伪类对象、后者是类加载器加载的真实类对象,二者类型语义不同、结构独立、不可互换。
Java 中基本类型(如 int、boolean)和它们的包装类(如 Integer、Boolean)在反射机制下各自拥有独立的 Class 对象,这种现象叫内存隔离——不是指物理内存被隔开,而是指 JVM 为它们分别创建了不同的 Class 实例,彼此不共享、不可互换。
这背后的关键在于:基本类型不是类,但 Java 为每种基本类型和 void 都定义了对应的 Class 对象;而包装类是真正的引用类型,有自己的 Class 对象。两者属于不同类别,JVM 在堆中为它们分别生成唯一的 Class 实例。
JVM 启动时就为八种基本类型(byte/short/int/long/float/double/char/boolean)和 void 创建了固定的 Class 对象。这些对象:
.class 文件产生的;int.class、Integer.TYPE 等方式获取,且恒等(== 比较为 true)。例如:
立即学习“Java免费学习笔记(深入)”;
System.out.println(int.class == Integer.TYPE); // trueSystem.out.println(int.class == Integer.class); // false —— 这是重点:int.class ≠ Integer.class
Integer.TYPE 是 int.class 的别名,而 Integer.class 是 java.lang.Integer 类加载后生成的 Class 对象,二者指向完全不同的结构。
Integer、Boolean 等是普通类,位于 java.lang 包下,其 Class 对象:
rt.jar(或 JDK 9+ 的模块系统)中加载;.class 字节码文件;Class 实例本身在堆中;public static 方法(如 parseInt)、访问 public final 字段(如 MAX_VALUE)等。所以:
System.out.println(Integer.class.getName()); // "java.lang.Integer"System.out.println(int.class.getName()); // "int"(注意:不是全限定名,无包路径)System.out.println(void.class.getName()); // "void"
名称不同、结构不同、用途不同——自然不互通。
Class.isPrimitive() 和类型语义JVM 严格区分原始类型与引用类型:
int.class.isPrimitive() 返回 true,Integer.class.isPrimitive() 返回 false;List<int> 合法语法都不存在(编译不通过),因为泛型只接受引用类型;method.invoke(obj, 100) 若方法声明为 void f(Integer x),传 int 值会触发自动装箱,但 Method 对象的 getParameterTypes() 返回的是 Integer.class,不是 int.class。这意味着:
Method m = clazz.getMethod("setAge", int.class); 和Method m2 = clazz.getMethod("setAge", Integer.class);比如读取配置文件决定调用哪个 setter:
paramType=intmethodName=setCount
若代码写成:
Class<?> type = Class.forName(config.get("paramType")); // ❌ 报错!"int" 不是合法类名
正确做法是预设映射:
Map<String, Class<?>> PRIMITIVE_MAP = Map.of( "int", int.class, "boolean", boolean.class, "double", double.class);Class<?> paramType = PRIMITIVE_MAP.get(config.get("paramType"));Method m = clazz.getMethod(config.get("methodName"), paramType);
否则,Class.forName("int") 会抛 ClassNotFoundException —— 因为 "int" 不是类的全限定名,只是 JVM 内部标识符。
不复杂但容易忽略