How does array class work in Java?

后端 未结 4 1224
感动是毒
感动是毒 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: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.

提交回复
热议问题