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

huangapple go评论81阅读模式
英文:

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. 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)]? 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)? This is what I read in the textbook I am learning from:

Creating and Accessing 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. For example, an array consisting of a collection of seven
variables of type double can be created as follows:

double[] temperature = new double[7];

This is like declaring the following seven strangely named variables
to have the type double:

temperature[0], temperature[1], temperature[2], temperature[3],
temperature[4], temperature[5], temperature[6]
英文:

I'm learning about Java arrays and I read something along the lines of that Java arrays are actually objects. 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)]? 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)?

This is what I read in the textbook I am learning from:

> Creating and Accessing 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. For example, an array consisting of a collection of seven
> variables of type double can be created as follows:
>
>
> double[] temperature = new double[7];
>

>
> This is like declaring the following seven strangely named variables
> to have the type double:
>
>
> temperature[0], temperature[1], temperature[2], temperature[3],
> temperature[4], temperature[5], temperature[6]
>

答案1

得分: 2

在分配数组的代码中:

int myInts = new int[20];

JVM将在堆中分配一个大小为20乘以整数大小(4字节)的块。数组通过索引偏移乘以基本对象大小进行访问/索引(同样在本例中为4字节)。

另外,我认为数组索引是直接内置到Java字节码中的。

--- 编辑 ---

关于数组索引是否内置:如果您在这里查看,可以确认aaload JBC命令/从数组中检索:https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings

英文:

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. The array is accessed / indexed using the index offset times the primative object size (again in this case, 4 bytes)

Also, I believe that array indexing is built directly into Java byte codes.

--- 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

答案2

得分: 2

Java中的数组是对象,但它们不是任何类的实例。没有数组类-请参阅关于对象类型-第4.3.1节

对象是类实例或数组。

数组对象类似于具有多个变量的类,但是包含10个双精度值的数组不是一个类。更像是,“双精度数组”是一种单一类型,实例变量的数量存储在对象本身内部,在数组的length字段中。

关于数组-第10节

在Java编程语言中,数组是对象(§4.3.1),是动态创建的,并且可以分配给Object类型的变量(§4.3.2)。

数组对象包含多个变量。变量的数量可以为零,此时数组被称为空。数组中包含的变量没有名称;而是通过使用非负整数索引值的数组访问表达式引用这些变量。这些变量被称为数组的组件。

数组在JVM中的实现方式是另一回事。在OpenJDK中,数组对象的内存布局包括指向“ArrayKlass”的指针,这是数组类型的内部表示。数组的大小包含在数组对象中,数组对象本身可以具有任意大小。这与作为类实例的对象不同:类实例始终具有相同的大小。

英文:

Arrays in Java are objects, but they are not instances of any class. There are no array classes -see
On the object types - section 4.3.1:

> An object is a class instance or an array.

Array objects are similar to classes with a number of variables, but an array of 10 doubles is not a 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.

On arrays - section 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).
>
> An array object contains a number of variables. 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. These variables are called the components of the array.

How arrays are implemented in a JVM is another matter. In OpenJDK, the memory layout for array objects includes a pointer to an "ArrayKlass" which is an internal representation for the array type. The size of the array is included within the array object, and the array objects themselves can have an arbitrary size. This is different from objects that are class instances: class instances are always the same size.

huangapple
  • 本文由 发表于 2020年7月31日 03:22:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63179972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定