Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is inheriting from a primitive array impossible from the JVM's perspective?

Well my doubt is this one:

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

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.

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

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

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

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

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

like image 269
TheStack Avatar asked Nov 08 '14 22:11

TheStack


People also ask

Is an array a primitive data type?

No, arrays are not primitive datatypes in Java. They are container objects which are created dynamically. All methods of class Object may be invoked on an array.

Is array primitive type in C#?

The one primitive type that hasn't been covered is the array. An array contains a fixed number of items, and each item is a value of the array's element type. The array elements are individually indexed starting from zero.

Is an array a primitive type or an Object in C ++?

I am from a java background, in java arrays are objects. I do know that an array in c++ could contain primitive data types or objects, too, I believe.

Can an array contain elements of an Object type as well as a primitive type?

Java arrays can store primitive types and strings, but cannot store any other type of object...


3 Answers

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

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.

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.

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?

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.)

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.

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

No. Array types implicitly inherit from 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.


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

  • 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.

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

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.

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:

  • 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,

  • and so on.

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

like image 112
Stephen C Avatar answered Oct 07 '22 21:10

Stephen C


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

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.

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

Yes, if you have two int[] arrays, say a and b, then 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.

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

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

like image 40
asteri Avatar answered Oct 07 '22 21:10

asteri


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

like image 27
kittylyst Avatar answered Oct 07 '22 21:10

kittylyst