java中getClass()、TYPE、class的区别

首先,基本数据类型只有.class,它们也没有对象,比如

System.out.println(int.class.getTypeName());  // 输出 int
System.out.println(int.class);                // 输出 int

getTypeName() 返回一个字符串,(返回 "int" 字符串),否则,int.class 返回的是 java.lang.Class!

那么基本数据类型的包装类,或者普通定义的类

它们的 .TYPE 就 相当于 基本数据类型的 .class

Integer integer = 2;
System.out.println(Integer.TYPE.getTypeName());    // 输出 int  字符串
System.out.println(Integer.TYPE);             // 输出 int

它们的 .class 输出其完整类路径, 就 相当于 它某个实例的 .getClass()

System.out.println(Integer.class.getTypeName());        // 输出 java.lang.Integer 字符串
System.out.println(Integer.class);                 // 输出 java.lang.Integer
System.out.println(integer.getClass().getTypeName());     // 输出  java.lang.Integer  字符串
System.out.println(integer.getClass());             // 输出 java.lang.Integer

 

posted @ 2021-06-08 13:07  tianyee  阅读(725)  评论(0编辑  收藏  举报