歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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

Copyright © Linux教程網 All Rights Reserved