简体   繁体   English

任何类型的 Java 数组在文件中实际上是什么样的?

[英]What does a Java array of any type actually look like in file?

I'm learning about Java arrays and I read something along the lines of that Java arrays are actually objects.我正在了解 Java arrays 并且我读到了一些类似 Java ZA3CCBC4F9D0CE2F2CD1955 个对象的内容。 Does that mean that when I make an array, say int[] array = new int[10] , the underlying mechanism is really just a class file that gets created with 10 instance variables of type int that I can access with array[(0-9)]?这是否意味着当我创建一个数组时,比如int[] array = new int[10] ,底层机制实际上只是一个 class 文件,它使用 10 个 int 类型的实例变量创建,我可以使用 array[(0 -9)]? If so, how does it do that and how does it work?如果是这样,它是如何做到的以及它是如何工作的? And what is the conceptual reason (if any) for the syntax to declare arrays the way it is (so I can remember the syntax rules easier)?以它的方式声明 arrays 的语法的概念原因(如果有的话)是什么(所以我可以更容易地记住语法规则)?

This is what I read in the textbook I am learning from:这是我在我正在学习的教科书中读到的:

Creating and Accessing Arrays创建和访问 Arrays

In Java, an array is a special kind of object, but it is often more useful to think of an array as a collection of variables of the same type.在 Java 中,数组是 object 的一种特殊类型,但将数组视为相同类型变量的集合通常更有用。 For example, an array consisting of a collection of seven variables of type double can be created as follows:例如,可以按如下方式创建由七个 double 类型变量的集合组成的数组:

 double[] temperature = new double[7];

This is like declaring the following seven strangely named variables to have the type double:这就像将以下七个奇怪命名的变量声明为 double 类型:

 temperature[0], temperature[1], temperature[2], temperature[3], temperature[4], temperature[5], temperature[6]

in the code where you allocate an array:在分配数组的代码中:

int myInts = new int[20];

The JVM will allocate a block the size of 20 times the size of an integer (4 bytes) in the heap. JVM 将在堆中分配一个大小为 integer(4 字节)大小 20 倍的块。 The array is accessed / indexed using the index offset times the primative object size (again in this case, 4 bytes)使用索引偏移乘以原始 object 大小(在本例中也是 4 个字节)访问/索引数组

Also, I believe that array indexing is built directly into Java byte codes.另外,我相信数组索引直接内置在 Java 字节码中。

--- Edit --- - - 编辑 - -

On array indexing being built in: If you look here, you can confirm the aaload JBC command / retrieve from an array: https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings关于内置数组索引:如果你看这里,你可以确认 aaload JBC 命令/从数组中检索: https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings

Arrays in Java are objects, but they are not instances of any class. Java 中的 Arrays 是对象,但它们不是任何 class 的实例。 There are no array classes -see On the object types - section 4.3.1 :没有数组类 - 请参阅 object 类型 - 第 4.3.1 节

An object is a class instance or an array. object 是 class 实例或数组。

Array objects are similar to classes with a number of variables, but an array of 10 doubles is not a class.数组对象类似于具有多个变量的类,但包含 10 个双精度的数组不是 class。 It's more like, "array of doubles" is one single type, and the number of instance variables is stored within the object itself, in the array's length field.更像是,“双精度数组”是一种单一类型,实例变量的数量存储在 object 本身的数组length字段中。

On arrays - section 10 : 在 arrays - 第 10 节

In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2).在 Java 编程语言中,arrays 是对象(§4.3.1),是动态创建的,并且可以分配给类型为 Z497031794414A552435F901531AC3B54BZ 的变量(§4.3.2)。

An array object contains a number of variables.数组 object 包含许多变量。 The number of variables may be zero, in which case the array is said to be empty.变量的数量可能为零,在这种情况下,数组被称为是空的。 The variables contained in an array have no names;数组中包含的变量没有名称; instead they are referenced by array access expressions that use non-negative integer index values.相反,它们被使用非负 integer 索引值的数组访问表达式引用。 These variables are called the components of the array.这些变量称为数组的组件。

How arrays are implemented in a JVM is another matter.如何在 JVM 中实现 arrays 是另一回事。 In OpenJDK, the memory layout for array objects includes a pointer to an "ArrayKlass" which is an internal representation for the array type.在 OpenJDK 中,数组对象的 memory 布局包括一个指向“ArrayKlass”的指针,它是数组类型的内部表示。 The size of the array is included within the array object, and the array objects themselves can have an arbitrary size.数组的大小包含在数组 object 中,数组对象本身可以具有任意大小。 This is different from objects that are class instances: class instances are always the same size.这与作为 class 实例的对象不同:class 实例的大小始终相同。

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

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