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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM