简体   繁体   English

数组是 Java 中的对象吗?

[英]How arrays are objects in Java?

As we know that arrays are objects, so what is the class behind creation of array objects.我们知道数组是对象,那么创建数组对象背后的类是什么。

When we write a statement, int a[]=new int[56];我们写语句的时候,int a[]=new int[56];

Here int is a primitive, it cannot be the class used for defining array objects,这里 int 是一个原语,它不能是用于定义数组对象的类,

Object can be the superclass of array objects, but it can't be the immediate class as it doesn't contain length field.对象可以是数组对象的超类,但不能是直接类,因为它不包含长度字段。 So please help me in understanding this fact.所以请帮助我理解这个事实。

JLS §4.3.1 defines an object as : JLS §4.3.1 将对象定义为

[...] a class instance or an array. [...] 类实例或数组。

" Object can be the superclass of array objects " - This is not quite correct. 对象可以是数组对象的超类”——这不太正确。 There is no explicit class for arrays and thus another class cannot extend an array.数组没有明确的类,因此另一个类不能扩展数组。 The element-type of an array is independent and can be either a primitive or an object.数组的元素类型是独立的,可以是原始类型,也可以是对象。

As to the question how the length of an array works, the JLS defines it in §10.3 :至于数组的length如何工作的问题, JLS 在 §10.3 中定义了它

[...] [...]

An array creation expression specifies the element type, the number of levels of nested arrays, and the length of the array for at least one of the levels of nesting.数组创建表达式指定元素类型、嵌套数组的级别数以及至少一层嵌套的数组长度。 The array's length is available as a final instance variable length .数组的长度可用作final实例变量length

[...] [...]

Arrays are builtin with Java itself, you can't find a class for it like you would for example String .数组是 Java 本身内置的,您无法像String那样为它找到一个类。

Arrays are Objects in java, that's true.数组是java中的对象,这是真的。

When we write a statement, int a[]=new int[56];我们写语句的时候,int a[]=new int[56];

Here int is a primitive, it cannot be the class used for defining array objects...这里 int 是一个原语,它不能是用于定义数组对象的类......

int does represent primitive type values, but the object(array) is not of int type it just contains elements of type int . int确实代表原始类型的值,但对象(阵列)是不int类型它只是包含类型的元素int

can you tell me how arr.length works if we say arr is an object如果我们说 arr 是一个对象,你能告诉我 arr.length 是如何工作的吗

Arrays ARE Objects as we already stated, length is a FIELD/ATTRIBUT E that every instance of array contains, that field is final and is created once you instantiate array:正如我们已经说过的,数组是对象,长度是一个字段/属性E,数组的每个实例都包含该字段,该字段是最终的,并且在实例化数组后创建:

int[] arrayOfInts = new int[ 23 ]; int[] arrayOfInts = new int[ 23 ];

As you can see above we specify 23 for the size of our int[] and that size will be assigned to length field when our array is created, and we already said it's final so it can't be reassigned, we can only create new array not resize the current one.正如您在上面看到的,我们为int[]的大小指定了 23,并且该大小将在创建数组时分配给length字段,并且我们已经说过它是最终的,因此不能重新分配,我们只能创建新的数组不调整当前的大小。

Hope I cleared some of your confusion.希望我清除了你的一些困惑。

To put it in not-too-technical terms, arrays are a special kind of object.用不太技术的术语来说,数组是一种特殊的对象。 It's not an explicitly named class defined somewhere in the standard library like Map or String .它不是在标准库中某处定义的显式命名类,如MapString It's more like something built into Java itself.它更像是内置于 Java 本身的东西。 It has its own special declaration syntax, but it behaves like all other objects.它有自己特殊的声明语法,但它的行为与所有其他对象一样。

Basically, for every primitive type or class defined, Java implicitly defines a corresponding array class that extends Object directly.基本上,对于定义的每个原始类型或类,Java 隐式定义了一个直接扩展Object的相应array类。 It provides a special syntax for creating and accessing the array, but other than that, it is a plain object, with fields and overridden methods from Object , though those methods aren't really implemented to do anything useful.它为创建和访问数组提供了一种特殊的语法,但除此之外,它是一个普通对象,具有来自Object字段和覆盖方法,尽管这些方法并没有真正实现来做任何有用的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM