tags:

views:

179

answers:

4

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().getName() gives strange result.

String[] array = new String[]{"one","two"};
System.out.println(array.getClass().getName()); // prints [Ljava.lang.String;

I want to understand how array works under the hood. Is the array class definition hardcoded in the JVM?

Any resources, books, links on this will be helpful.

Thank you.

+5  A: 

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.

Jon Skeet
+1  A: 

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.

Sean Owen
+1  A: 

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[].

Jesper
+2  A: 

Regarding array class names

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 or void.

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;

Regarding array class members

JLS 10.7 Array members

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 class Object and throws no checked exceptions
  • All the members inherited from class Object; the only method of Object that is not inherited is its clone method

Related questions

polygenelubricants