java.lang.Class.isPrimitive()用法解析

一、概述:

此方法主要用来判断Class是否为原始类型(boolean、char、byte、short、int、long、float、double)。

二、格式:

Class.isPrimitive(),原始类型下返回true

三、示例:

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());
}

结果输出:

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