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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM