如何检查一个类是否表示特定的原始类型

huangapple go评论62阅读模式
英文:

How do I check if a class represents a particular primitive type

问题

我需要确定特定的类是否为特定的原始类型,我这里是指 int 类型。我找到了 Class.isPrimitive() 方法,但我不需要检查所有的原始类型,我只需要特定的一个。我注意到类名与原始类型名相同,我考虑过使用 candidateClass.getName().equals("int") 进行检查,但这似乎是一种不好的做法(还涉及到“魔术字符串”的使用)。

如何使用Java API正确检查特定的原始类型?

英文:

I need to determine whether a specific class is a particular primitive type, an int in my case. I found Class.isPrimitive() method, but I don't need to check all primitives, I need a particular one. I noticed that the class names are equal to primitive names and I considered checking for candidateClass.getName().equals("int") - but it seemed a bad practice (and a "magic string" use).

How to check for a specific primitive type properly using Java API?

答案1

得分: 5

每个原始类型的包装器都提供一个静态字段,其中包含这些包装器的原始类型的类。例如:Integer.TYPE

关于在Java中实现原始类型类的信息,可以查阅Class.isPrimitive的文档:

有九个预定义的Class对象,用于表示八种原始类型和void类型。这些对象由Java虚拟机创建,其名称与它们所代表的原始类型的名称相同,即boolean、byte、char、short、int、long、float和double。
只能通过以下public static final变量访问这些对象,并且这些对象是唯一使得该方法返回true的Class对象。

以下是一些参考代码:

public class PrimitiveClassTest {
    static class Foo{
       public int a;
    }
    
    public static void main(String[] args) throws NoSuchFieldException, SecurityException {
        Class<?> intClass = Foo.class.getField("a").getType();
        System.out.println("obj.int field class: "+intClass);
        System.out.println("Integer.TYPE class:  "+Integer.TYPE);
        System.out.println("are they equal?      "+Integer.TYPE.equals(intClass));
    }
}

该代码将产生以下结果:

obj.int field class: int
Integer.TYPE class:  int
are they equal?      true
英文:

Each wrapper for each primitive type provides a static field which contains classes of primitive types for those wrappers. For example: Integer.TYPE.

The documentation of Class.isPrimitive provides information about the implementation of primitive types classes in Java:

> There are nine predefined Class objects to representthe eight primitive types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types that they represent, namely boolean, byte, char, short, int, long, float, and double.
> These objects may only be accessed via the following public static final variables, and are the only Class objects for which this method returns true.

Some code for reference:

public class PrimitiveClassTest {
    static class Foo{
       public int a;
    }
    
    public static void main(String[] args) throws NoSuchFieldException, SecurityException {
        Class&lt;?&gt; intClass = Foo.class.getField(&quot;a&quot;).getType();
        System.out.println(&quot;obj.int field class: &quot;+intClass);
        System.out.println(&quot;Integer.TYPE class:  &quot;+Integer.TYPE);
        System.out.println(&quot;are they equal?      &quot;+Integer.TYPE.equals(intClass));
    }
}

which gives the following results:

obj.int field class: int
Integer.TYPE class:  int
are they equal?      true

答案2

得分: 1

这可以大大简化为:

 intClass == int.class

这样做的原因是 Class 文档中提到:

> 基本的Java类型(boolean、byte、char、short、int、long、float和double),以及关键字void也表示为Class对象。

英文:

This can be greatly simplified to:

 intClass == int.class

This works because the Class documentation says:

> The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

huangapple
  • 本文由 发表于 2020年10月1日 20:09:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64155104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定