从JVM的角度来看,是否继承了原始数组?

时间:2021-04-25 17:21:50

Well my doubt is this one:

我的疑问是这个:

In Java, it is disallowed to inherit from an array, ie, one can't do things like:

在Java中,不允许从数组继承,即,不能执行以下操作:

class FloatVec extends float[]
{
    // Vector methods.
}

FloatVec somevec = new FloatVec()[] { 1, 2, 3 }; // With array initializer.

Or even better:

甚至更好:

class FloatVec3 extends float[3]
{
    // Regular accessor.
    public float getX() {
        return this[0];
    }
    // Or say, make it the 'this' implicit like with other fields:
    public void setVec(float x, float y, float z) {
        [0] = x;
        [1] = y;
        [2] = z;
    }
    // And specific vec3 methods like:
    public float dotProduct() {
        float x = this[0];
        float y = this[1];
        float z = this[2];
        return x * x + y * y + z * z;
    }
}

But arrays actually implement a specific interface and are considered objects. ie, one expects of array instances the same methods exposed by Object, plus a specific array field, the final 'length' field.

但是数组实际上实现了特定的接口并被视为对象。也就是说,人们期望数组实例与Object公开的方法相同,加上特定的数组字段,即最终的“长度”字段。

So my question are, even if the Java language disallows this usage:

所以我的问题是,即使Java语言不允许这种用法:

  • Could it be implemented in the JVM without too many changes?

    它可以在没有太多更改的情况下在JVM中实现吗?

  • Does the JVM treats arrays like any object with a class that can be inherited from?

    JVM是否将数组视为具有可继承的类的任何对象?

  • Does the JVM treats arrays like enums, ie, making the array objects inherit automatically from a defined array class?

    JVM是否将数组视为枚举,即使数组对象从定义的数组类中自动继承?

  • Is the array class defined somewhere in such a way that it could be inherited from?

    数组类是以某种方式定义的,它可以从中继承吗?

3 个解决方案

#1


9  

Could it be implemented in the JVM without too many changes?

它可以在没有太多更改的情况下在JVM中实现吗?

No. The changes would be massive. They would impact the entire Java tool chain, and also a huge body of 3rd-party code the works at or below the JLS / JVM abstraction layer.

不,这些变化将是巨大的。它们会影响整个Java工具链,也会影响JLS / JVM抽象层或其下方的大量第三方代码。

There is nothing stopping you from downloading the (OpenJDK) source code and trying to do this yourself as an experiment. But the chances of this happening in real Java are (IMO) vanishingly small.

没有什么可以阻止您下载(OpenJDK)源代码并尝试自己做这个实验。但真正Java中发生这种情况的可能性很小(IMO)。

I've listed just some of the technical problems at the bottom. I'm sure that there are others.

我在最底层列出了一些技术问题。我确信还有其他人。

Does the JVM treats arrays like any object with a class that can be inherited from?

JVM是否将数组视为具有可继承的类的任何对象?

No.

没有。

Array types in Java provide a small number of methods. (The methods are getClass, hashCode, toString, clone, wait, notify and notifyAll ... as per the Object API.)

Java中的数组类型提供了少量方法。 (方法是getClass,hashCode,toString,clone,wait,notify和notifyAll ...根据Object API。)

As far as I know, the actual implementations of these methods are provided by the native methods defined by java.lang.Object. But since the JLS does no permit you to write code that inherits from an array, the JVM spec doesn't need to provide a way to implement such code. And, in practice, it doesn't.

据我所知,这些方法的实际实现是由java.lang.Object定义的本机方法提供的。但由于JLS不允许您编写从数组继承的代码,因此JVM规范不需要提供实现此类代码的方法。而且,在实践中,它没有。

Does the JVM treats arrays like enums, ie, making the array objects inherit automatically from a defined array class?

JVM是否将数组视为枚举,即使数组对象从定义的数组类中自动继承?

No. Array types implicitly inherit from java.lang.Object.

否。数组类型隐式继承自java.lang.Object。

Is the array class defined somewhere in such a way that it could be inherited from?

数组类是以某种方式定义的,它可以从中继承吗?

No such class exists. Array types implicitly inherit from Object.

没有这样的类。数组类型隐式继承自Object。


At the JVM spec level, there are a couple of major impediments to making arrays more "class-like":

在JVM规范级别,使阵列更像“类似”有几个主要障碍:

  • The "type string" representation of an array type ("[elem-type") only mentions the element. The actual array type has no name, and if it did then the "Lname;" representation is saying that this is a regular class, not an array type.

    数组类型(“[elem-type”)的“类型字符串”表示仅提及该元素。实际的数组类型没有名称,如果是,那么“Lname;”表示这是一个常规类,而不是数组类型。

  • Arrays are created by special JVM instructions that provide the element type as an operand not the array type.

    数组由特殊的JVM指令创建,这些指令将元素类型提供为操作数而不是数组类型。

And beyond that, a JVM implementation is going to assume things about how arrays work in order to implement them efficiently. Even though (according to the JLS) it appears to theoretically possible to use an invoke instruction to call a non-standard method on an array, a JVM interpreter or JIT compiler wouldn't know what to make of it ... even if you somehow managed to sneak the invoke past the class loader and verifier.

除此之外,JVM实现将假设数组如何工作以便有效地实现它们。即使(根据JLS)理论上似乎可以使用invoke指令来调用数组上的非标准方法,JVM解释器或JIT编译器也不知道该怎么做...即使你不知何故设法通过类加载器和验证器潜行调用。

Once you get past that, then there is that problem that if arrays could be declared with user-defined superclasses, then those superclasses would (presumably) be able to have instance variables. But that implies that the JVM spec needs to change so that:

一旦你越过那个,那么就存在这样的问题,即如果数组可以用用户定义的超类声明,那么那些超类(可能)将能够拥有实例变量。但这意味着JVM规范需要改变,以便:

  • the heap nodes for arrays can hold instance variables as well as the array itself,

    数组的堆节点可以包含实例变量以及数组本身,

  • the regular field load and store instructions work on array objects as well as reculare objects,

    常规字段加载和存储指令适用于数组对象以及reculare对象,

  • and so on.

    等等。

As you can see, this "small extension" unravels a whole lot of other design decisions in the JVM.

正如您所看到的,这个“小扩展”在JVM中揭示了许多其他设计决策。

#2


6  

Does the JVM treats arrays like any object with a class that can be inherited from?

JVM是否将数组视为具有可继承的类的任何对象?

Each primitive array type does indeed have a class. For example, an int[] actually is an instance of int[].class and a float[] is an instance of float[].class. (JLS 10.8) You can see in your own code sample that these classes cannot be inherited from.

每个原始数组类型确实都有一个类。例如,int []实际上是int []的实例.class和float []是float [] .class的实例。 (JLS 10.8)您可以在自己的代码示例中看到这些类无法继承。

Does the JVM treats arrays like enums, ie, making the array objects inherit automatically from a defined array class?

JVM是否将数组视为枚举,即使数组对象从定义的数组类中自动继承?

Yes, if you have two int[] arrays, say a and b, then a.getClass() == b.getClass().

是的,如果你有两个int []数组,比如a和b,那么a.getClass()== b.getClass()。

Is the array class defined somewhere in such a way that it could be inherited from?

数组类是以某种方式定义的,它可以从中继承吗?

As you can see in your own code example, these classes cannot be inherited from. They have no source code and are created by the JVM itself, as said in this other answer along with a code example.

正如您在自己的代码示例中所看到的,这些类无法继承。它们没有源代码,并且由JVM本身创建,正如另一个答案和代码示例所述。

Could it be implemented in the JVM without too many changes?

它可以在没有太多更改的情况下在JVM中实现吗?

This question is outside of the scope of Stack Overflow and is very subjective.

这个问题超出了Stack Overflow的范围,并且非常主观。

#3


1  

The closest that you could get to this in Java is Gil Tene's ObjectLayout project - http://objectlayout.org/ - this is still a long way from making it into Java, but it's just possible that this will make it as part of Java 9

你可以在Java中得到的最接近的是Gil Tene的ObjectLayout项目 - http://objectlayout.org/ - 从使它成为Java还有很长的路要走,但这可能会使它成为Java的一部分9

#1


9  

Could it be implemented in the JVM without too many changes?

它可以在没有太多更改的情况下在JVM中实现吗?

No. The changes would be massive. They would impact the entire Java tool chain, and also a huge body of 3rd-party code the works at or below the JLS / JVM abstraction layer.

不,这些变化将是巨大的。它们会影响整个Java工具链,也会影响JLS / JVM抽象层或其下方的大量第三方代码。

There is nothing stopping you from downloading the (OpenJDK) source code and trying to do this yourself as an experiment. But the chances of this happening in real Java are (IMO) vanishingly small.

没有什么可以阻止您下载(OpenJDK)源代码并尝试自己做这个实验。但真正Java中发生这种情况的可能性很小(IMO)。

I've listed just some of the technical problems at the bottom. I'm sure that there are others.

我在最底层列出了一些技术问题。我确信还有其他人。

Does the JVM treats arrays like any object with a class that can be inherited from?

JVM是否将数组视为具有可继承的类的任何对象?

No.

没有。

Array types in Java provide a small number of methods. (The methods are getClass, hashCode, toString, clone, wait, notify and notifyAll ... as per the Object API.)

Java中的数组类型提供了少量方法。 (方法是getClass,hashCode,toString,clone,wait,notify和notifyAll ...根据Object API。)

As far as I know, the actual implementations of these methods are provided by the native methods defined by java.lang.Object. But since the JLS does no permit you to write code that inherits from an array, the JVM spec doesn't need to provide a way to implement such code. And, in practice, it doesn't.

据我所知,这些方法的实际实现是由java.lang.Object定义的本机方法提供的。但由于JLS不允许您编写从数组继承的代码,因此JVM规范不需要提供实现此类代码的方法。而且,在实践中,它没有。

Does the JVM treats arrays like enums, ie, making the array objects inherit automatically from a defined array class?

JVM是否将数组视为枚举,即使数组对象从定义的数组类中自动继承?

No. Array types implicitly inherit from java.lang.Object.

否。数组类型隐式继承自java.lang.Object。

Is the array class defined somewhere in such a way that it could be inherited from?

数组类是以某种方式定义的,它可以从中继承吗?

No such class exists. Array types implicitly inherit from Object.

没有这样的类。数组类型隐式继承自Object。


At the JVM spec level, there are a couple of major impediments to making arrays more "class-like":

在JVM规范级别,使阵列更像“类似”有几个主要障碍:

  • The "type string" representation of an array type ("[elem-type") only mentions the element. The actual array type has no name, and if it did then the "Lname;" representation is saying that this is a regular class, not an array type.

    数组类型(“[elem-type”)的“类型字符串”表示仅提及该元素。实际的数组类型没有名称,如果是,那么“Lname;”表示这是一个常规类,而不是数组类型。

  • Arrays are created by special JVM instructions that provide the element type as an operand not the array type.

    数组由特殊的JVM指令创建,这些指令将元素类型提供为操作数而不是数组类型。

And beyond that, a JVM implementation is going to assume things about how arrays work in order to implement them efficiently. Even though (according to the JLS) it appears to theoretically possible to use an invoke instruction to call a non-standard method on an array, a JVM interpreter or JIT compiler wouldn't know what to make of it ... even if you somehow managed to sneak the invoke past the class loader and verifier.

除此之外,JVM实现将假设数组如何工作以便有效地实现它们。即使(根据JLS)理论上似乎可以使用invoke指令来调用数组上的非标准方法,JVM解释器或JIT编译器也不知道该怎么做...即使你不知何故设法通过类加载器和验证器潜行调用。

Once you get past that, then there is that problem that if arrays could be declared with user-defined superclasses, then those superclasses would (presumably) be able to have instance variables. But that implies that the JVM spec needs to change so that:

一旦你越过那个,那么就存在这样的问题,即如果数组可以用用户定义的超类声明,那么那些超类(可能)将能够拥有实例变量。但这意味着JVM规范需要改变,以便:

  • the heap nodes for arrays can hold instance variables as well as the array itself,

    数组的堆节点可以包含实例变量以及数组本身,

  • the regular field load and store instructions work on array objects as well as reculare objects,

    常规字段加载和存储指令适用于数组对象以及reculare对象,

  • and so on.

    等等。

As you can see, this "small extension" unravels a whole lot of other design decisions in the JVM.

正如您所看到的,这个“小扩展”在JVM中揭示了许多其他设计决策。

#2


6  

Does the JVM treats arrays like any object with a class that can be inherited from?

JVM是否将数组视为具有可继承的类的任何对象?

Each primitive array type does indeed have a class. For example, an int[] actually is an instance of int[].class and a float[] is an instance of float[].class. (JLS 10.8) You can see in your own code sample that these classes cannot be inherited from.

每个原始数组类型确实都有一个类。例如,int []实际上是int []的实例.class和float []是float [] .class的实例。 (JLS 10.8)您可以在自己的代码示例中看到这些类无法继承。

Does the JVM treats arrays like enums, ie, making the array objects inherit automatically from a defined array class?

JVM是否将数组视为枚举,即使数组对象从定义的数组类中自动继承?

Yes, if you have two int[] arrays, say a and b, then a.getClass() == b.getClass().

是的,如果你有两个int []数组,比如a和b,那么a.getClass()== b.getClass()。

Is the array class defined somewhere in such a way that it could be inherited from?

数组类是以某种方式定义的,它可以从中继承吗?

As you can see in your own code example, these classes cannot be inherited from. They have no source code and are created by the JVM itself, as said in this other answer along with a code example.

正如您在自己的代码示例中所看到的,这些类无法继承。它们没有源代码,并且由JVM本身创建,正如另一个答案和代码示例所述。

Could it be implemented in the JVM without too many changes?

它可以在没有太多更改的情况下在JVM中实现吗?

This question is outside of the scope of Stack Overflow and is very subjective.

这个问题超出了Stack Overflow的范围,并且非常主观。

#3


1  

The closest that you could get to this in Java is Gil Tene's ObjectLayout project - http://objectlayout.org/ - this is still a long way from making it into Java, but it's just possible that this will make it as part of Java 9

你可以在Java中得到的最接近的是Gil Tene的ObjectLayout项目 - http://objectlayout.org/ - 从使它成为Java还有很长的路要走,但这可能会使它成为Java的一部分9