当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.lang.Class.isPrimitive()用法及代码示例



描述

这个java.lang.Class.isPrimitive()确定指定的 Class 对象是否表示原始类型。有九个预定义的 Class 对象来表示八种原始类型和 void。它们由 Java 虚拟机创建,并与它们所代表的原始类型具有相同的名称,即 boolean、byte、char、short、int、long、float 和 double。

声明

以下是声明java.lang.Class.isPrimitive()方法

public boolean isPrimitive()

参数

NA

返回值

当且仅当此类表示原始类型时,此方法才返回 true。

异常

NA

示例

下面的例子展示了 java.lang.Class.isPrimitive() 方法的用法。

package com.tutorialspoint;

import java.lang.*;

public class ClassDemo {

   public static void main(String[] args) {

      // returns the Class object associated with this class
      ClassDemo cl = new ClassDemo();
      Class c1Class = cl.getClass();

      // returns the Class object associated with an integer
      int k = 5;
      Class kClass = int.class;

      // checking for primitive type
      boolean retval1 = c1Class.isPrimitive();
      System.out.println("c1 is primitive type? = " + retval1);

      // checking for primitive type?
      boolean retval2 = kClass.isPrimitive();
      System.out.println("k is primitive type? = " + retval2);
   }
}

让我们编译并运行上面的程序,这将产生以下结果——

c1 is primitive type? = false
k is primitive type? = true

相关用法


注:本文由纯净天空筛选整理自 Java.lang.Class.isPrimitive() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。