Java Is it possible to subclass an array? And more questions about arrays in Java

These questions are simply asked out of curiosity. I don't need to subclass the array, I'm just trying to figure out how they work in Java.

  • Where is the Javadoc API for arrays? I found it for the "Arrays" class, but this class just contains utilities for use on Java arrays and is not the actual array class. This leads me to the following question:

  • Is there an actual array class all of which arrays are subclasses?

  • Is Object[] superclass of String[] (for example)? I guess the answer is no here. Are these actual classes similar to any other class?

  • Is String[] another class from String[][] ? Or String[][][] , etc.

  • As specified in the header, is it possible to subclass an array class (or a subclass of an array class? Not sure yet how it works, as you can tell with my previous poll)? Can I create my own class, instances of which were executed exactly the same as arrays, except that they had more functionality?

Thanks.

+4
source share
3 answers

The Java language specification answers all these questions:

The direct superclass of the array type is Object. Each array type implements the Cloneable and java.io.Serializable interfaces.

No, there is no common base class for all arrays and, therefore, there is no JavaDoc. The members of arrays are defined in the specification.

The subtyping between array types is defined in section 4.10.3 - and yes, String[] is a subtype of Object[] . See Also ArrayStoreException .

Yes, String[].class != String[][].class . (cf section 10.8 )

No, there is no syntax for subclass arrays. In particular, an extends clause must contain a class type (and array types are not class types).

+7
source

There is no array class in Java.

Interestingly, arrays are objects (but not class instances):

An object is an instance of a class or an array.

More details here: http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html

Classes that implement java.util.List provide a more object-oriented implementation of structures like arrays.

+2
source

You cannot subclass arrays. Although the syntax used with them is slightly different, they are objects (check with JLS ). There are not many APIs for them - except that Object (with toString() not doing what you expect, use Arrays.deepToString() for this; equals() and hashCode() are similar) have a length field. In addition, arrays are cloned. You can use array types only if the type of the target element is a supertype of the type of the source element - you can drop String[] to Object[] , but not vice versa. If you are sure that the objects in the array are of a particular type, you can use each element separately. String[][] is an array from String[] , so it differs from String[] because its elements are String arrays, not strings. You can create classes that provide similar functionality to arrays ( ArrayList does just that), but they will not be interchangeable with regular arrays.

-one
source

Source: https://habr.com/ru/post/1399236/


All Articles