5

These questions are purely asked out of curiosity. I don't actually need to subclass an array, I'm just trying to figure out more about how they work in Java.

  1. Where is the Javadoc API for arrays? I found one for the 'Arrays' class, but that class just contains utilities to use on Java arrays, and is not the actual array class. This leads me to my next question:

  2. IS there an actual array class of which all arrays are subclasses?

  3. Is Object[] a superclass of String[] (for example)? I'm guessing the answer here is no. Are these actual classes like any other class?

  4. Is String[] a different class from String[][]? Or String[][][], etc?

  5. As asked in the title, is it possible to subclass an array class (or to subclass THE array class? still not sure how it works as you can tell by my above questioning)? Could I create my own class, instances of which acted exactly like arrays, except they had more functionality?

Thanks.

1
  • 4
    Most of these questions can be answered with a short program and using instanceof. Did you try it? Commented Mar 1, 2012 at 17:38

3 Answers 3

8

The Java Language Specification answers all these questions:

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

So no, there isn't a common base class for all arrays, and therefore no JavaDoc either. The members of arrays are defined by the spec instead.

Subtyping among 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. (c.f. section 10.8)

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

Sign up to request clarification or add additional context in comments.

1 Comment

direct superclass of an array type is Object, so how String[] is subtype of Object[]. first sentence means that String[] extends Object not Object[]. I am confused
2

There is no array class in Java.

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

An object is a class instance or an array.

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

The classes implementing java.util.List provide a more object-oriented implementation of array-like structures.

1 Comment

Arrays are objects in Java, though the syntax used with them is somewhat different from other objects (check the JLS).
-1

You can't subclass arrays. Even though the syntax used with them is a bit different, they are objects (check with the JLS). There's not much API to them - apart from just what Object has (with toString() not doing what you expect, use Arrays.deepToString() for that; equals() and hashCode() are similar) there's the length field. Additionally, arrays are cloneable. You can only cast array types if the target element type is a supertype of the source element type - you can cast String[] to Object[] but not the other way around. If you are sure the objects in the array are a specific type, you can cast each element individually. String[][] is an array of String[], so it's a different type than String[] as its elements are arrays of String, not Strings. You can create classes which give similar functionality to arrays (ArrayList does just that), but they will not be interchangeable with regular arrays.

2 Comments

If you must paraphrase the spec, please do so correctly. You forgot the length field and the clone method, and the interfaces Serializable and Clonable. Also, String[] is a subtype of Object[].
Fixed based on you feedback. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.