In Java, array is a class and extends Object. I am curious to know about this special array class. I don\'t find the class definition anywhere. Doing a getClass().getNam
Yes, basically arrays are something the VM knows about intimately, like the primitive types. There are specific bytecode instructions to work with arrays - creating them, indexing into them etc.
As for resources to find out more - the JVM specification is probably the best starting point. Section 7.9 has some examples of bytecode working with arrays.
Yes, there is a synthetic Class
in the JVM representing an array of each possible type (like Integer[].class
). There is one for each primitive type too (like int[].class
). I don't think it's something that has a definition, like source file, anywhere. They behave kind of as expected; Number[].class
is assignable from Integer[].class
for instance.
It doesn't have special methods, if that's what you mean, nor a special source file anywhere. I don't even think the length
field that array types have is considered defined in that class either, though I admit I didn't check; that is special-cased by the VM.
JLS 10.8 defines this.
The Java Virtual Machine Specification explains in section 4.2 what names such as [Ljava.lang.String;
mean. See: 4.2 The Internal Form of Fully Qualified Class and Interface Names.
In this case the [
means this is an array type, and Lclassname;
indicates that this is an array of references to objects of type classname.
So [Ljava.lang.String;
means String[]
.
Class.getName clearly specifies these "funky" array names:
If this class object represents a reference type that is not an array type then the binary name of the class is returned, as specified by the Java Language Specification, Second Edition.
If this class object represents a primitive type or void, then the name returned is a
String
equal to the Java language keyword corresponding to the primitive type orvoid
.If this class object represents a class of arrays, then the internal form of the name consists of the name of the element type preceded by one or more
'['
characters representing the depth of the array nesting. The encoding of element type names is as follows:Element Type Encoding boolean Z byte B char C double D float F int I long J short S class or interface Lclassname;
The members of an array type are all of the following:
- The
public final field length
, which contains the number of components of the array (length may be positive or zero)- The
public method clone
, which overrides the method of the same name in classObject
and throws no checked exceptions- All the members inherited from
class Object
; the only method ofObject
that is not inherited is itsclone
method