原始类型类的用途是什么?

时间:2022-01-10 16:27:12

I recently learned that there are Class representations for the primitive types in the JVM. For example, int.class, double.class, and even a void.class.

我最近了解到JVM中的基本类型有类表示。例如,int.class、双。课程,甚至是一个void。

What I don't understand is why these are there. They don't seem to serve any functional role. Using reflection, I searched through the classes, and they have no constructors, no methods, and no fields. For all intents and purposes, they seem empty and useless. The primitive type variables are not even instances of their respective classes, as indicated by the following returning false:

我不明白为什么会有这些。它们似乎没有任何作用。使用反射,我搜索了这些类,它们没有构造函数、方法和字段。无论出于什么意图和目的,它们看起来都是空虚和无用的。原始类型变量甚至不是它们各自类的实例,如以下返回false所示:

int a = 3;
int.class.isInstance(a);

So why do they exist? They must serve some purpose, maybe for the compiler or something, but whatever it is is completely beyond me. There is even an explicit reference to int.class in the Integer API (and likewise for each primitive type and its respective wrapper Object). I haven't been able to find any reference to their existence, much less their use, in the JLS.

那么它们为什么存在呢?它们必须是有目的的,也许是编译器或者别的什么东西,但无论它是什么,我都无法理解。整型API中甚至有一个显式的int.class引用(每个基元类型及其各自的包装器对象也是如此)。我还没能找到他们的存在,更不用说他们在JLS中的使用了。

2 个解决方案

#1


12  

What I don't understand is why these are there.

我不明白的是为什么会有这些。

Consider the following:

考虑以下:

public int foo() {
    return 0;
}

...

Method method = someClass.getDeclaredMethod("foo");
Class<?> clazz = method.getReturnType();

Without a Class representation of int, what would the above return? It shouldn't return Integer.class as they're not the same thing. (Imagine trying to distinguish between methods which were overloaded, one with an int and one with an Integer parameter.)

如果没有int的类表示,上面的返回值是多少?它不应该返回整数。因为它们不是一回事。(想象一下,尝试区分重载的方法,一个是int型的,另一个是Integer参数的。)

I've used these classes before to provide default values for arguments when calling them via reflection. Based on the parameter type, I've used null for any reference type, and some (boxed, obviously) primitive value for each of the primitive types.

在通过反射调用参数时,我曾使用这些类为参数提供默认值。基于参数类型,我对任何引用类型都使用了null,对每个基元类型都使用了一些(显然是装箱的)基元值。

#2


2  

It is a cheap ass solution that turns out badly.

这是一种结果很糟糕的廉价解决方案。

Before 1.5, Java types can be categorized as

在1.5之前,Java类型可以分类为。

java type
    primitive type
    reference type
        class type (including interface)
        array type

Then ideally, java reflection should provide 5 concepts mirroring these 5 types. But they used a single Class to represent them all, including primitive and array types. So a Class does not necessarily mean a class.

然后,理想情况下,java反射应该提供5个概念,镜像这5个类型。但是他们使用一个类来表示它们,包括原始数组和数组类型。所以类不一定是类。

That's still manageable. But after 1.5, Java types become more complicated, so a new Type is introduced. Unfortunately, instead of having a new and clean hierarchy that directly mirror language spec, they decides to make Class a subtype of Type; not only the old mess is brought in, it spawns some new mess, and the whole Type hierarchy is unintelligible.

这仍然是可控的。但是在1.5之后,Java类型变得更加复杂,因此引入了一种新的类型。不幸的是,他们没有使用直接反映语言规范的新的、干净的层次结构,而是决定将类作为类型的子类型;不仅带来了旧的混乱,还产生了一些新的混乱,整个类型层次结构是不可理解的。

#1


12  

What I don't understand is why these are there.

我不明白的是为什么会有这些。

Consider the following:

考虑以下:

public int foo() {
    return 0;
}

...

Method method = someClass.getDeclaredMethod("foo");
Class<?> clazz = method.getReturnType();

Without a Class representation of int, what would the above return? It shouldn't return Integer.class as they're not the same thing. (Imagine trying to distinguish between methods which were overloaded, one with an int and one with an Integer parameter.)

如果没有int的类表示,上面的返回值是多少?它不应该返回整数。因为它们不是一回事。(想象一下,尝试区分重载的方法,一个是int型的,另一个是Integer参数的。)

I've used these classes before to provide default values for arguments when calling them via reflection. Based on the parameter type, I've used null for any reference type, and some (boxed, obviously) primitive value for each of the primitive types.

在通过反射调用参数时,我曾使用这些类为参数提供默认值。基于参数类型,我对任何引用类型都使用了null,对每个基元类型都使用了一些(显然是装箱的)基元值。

#2


2  

It is a cheap ass solution that turns out badly.

这是一种结果很糟糕的廉价解决方案。

Before 1.5, Java types can be categorized as

在1.5之前,Java类型可以分类为。

java type
    primitive type
    reference type
        class type (including interface)
        array type

Then ideally, java reflection should provide 5 concepts mirroring these 5 types. But they used a single Class to represent them all, including primitive and array types. So a Class does not necessarily mean a class.

然后,理想情况下,java反射应该提供5个概念,镜像这5个类型。但是他们使用一个类来表示它们,包括原始数组和数组类型。所以类不一定是类。

That's still manageable. But after 1.5, Java types become more complicated, so a new Type is introduced. Unfortunately, instead of having a new and clean hierarchy that directly mirror language spec, they decides to make Class a subtype of Type; not only the old mess is brought in, it spawns some new mess, and the whole Type hierarchy is unintelligible.

这仍然是可控的。但是在1.5之后,Java类型变得更加复杂,因此引入了一种新的类型。不幸的是,他们没有使用直接反映语言规范的新的、干净的层次结构,而是决定将类作为类型的子类型;不仅带来了旧的混乱,还产生了一些新的混乱,整个类型层次结构是不可理解的。