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 给外婆敬酒祝福语简短