此方法主要用來判斷Class是否爲原始類型(boolean、char、byte、short、int、long、float、double)。code
Class.isPrimitive(),原始類型下返回truestring
public static void main(String[] args){ Class stringClass=String.class; System.out.println("String is primitive type:"+stringClass.isPrimitive()); Class booleanClass=Boolean.class; System.out.println("Boolean is primitive type:"+booleanClass.isPrimitive()); Class booleanType=boolean.class; System.out.println("boolean is primitive type:"+booleanType.isPrimitive()); Class byteType=byte.class; System.out.println("byte is primitive type:"+byteType.isPrimitive()); Class charType=char.class; System.out.println("char is primitive type:"+charType.isPrimitive()); Class shortType=short.class; System.out.println("short is primitive type:"+shortType.isPrimitive()); Class intType=int.class; System.out.println("int is primitive type:"+intType.isPrimitive()); Class longType=long.class; System.out.println("long is primitive type:"+longType.isPrimitive()); Class floatType=float.class; System.out.println("float is primitive type:"+floatType.isPrimitive()); Class doubleType=double.class; System.out.println("double is primitive type:"+doubleType.isPrimitive()); }
結果輸出:it
String is primitive type:false Boolean is primitive type:false boolean is primitive type:true byte is primitive type:true char is primitive type:true short is primitive type:true int is primitive type:true long is primitive type:true float is primitive type:true double is primitive type:true