Java如何检查类是否表示原始类型?
Java使用类对象来表示所有八种基本类型。可以使用isPrimitive()方法调用标识表示原始类型的类对象。void在Java中不是类型,但是isPrimitive()方法为void.class返回true。
package org.nhooo.example.lang.reflect;
public class IsPrimitiveDemo {
public static void main(String[] args) {
IsPrimitiveDemo.get(int.class);
IsPrimitiveDemo.get(String.class);
IsPrimitiveDemo.get(double.class);
IsPrimitiveDemo.get(void.class);
}
private static void get(Class clazz) {
if (clazz.isPrimitive()) {
System.out.println(clazz.getName() +
" is a primitive type.");
} else {
System.out.println(clazz.getName() +
" is not a primitive type.");
}
}
}这是程序的结果:
int is a primitive type. java.lang.String is not a primitive type. double is a primitive type. void is a primitive type.
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短