How does array class work in Java?

后端 未结 4 1217
感动是毒
感动是毒 2020-12-10 16:52

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

相关标签:
4条回答
  • 2020-12-10 17:08

    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.

    0 讨论(0)
  • 2020-12-10 17:24

    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.

    0 讨论(0)
  • 2020-12-10 17:27

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

    0 讨论(0)
  • 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

    • dot length in java - finding its definition
    0 讨论(0)
提交回复
热议问题