Java 中的数组及其在内存中的存储方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5564423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Arrays in Java and how they are stored in memory
提问by rubixibuc
I'm trying to understand the array setup in java. Why must you initialize space for each each object in the array, after you have created the array. How is it stored in memory like this:
我试图了解 java 中的数组设置。为什么必须在创建数组后为数组中的每个对象初始化空间。它是如何像这样存储在内存中的:
[object][object]
or like this:
或者像这样:
[*class]->[object]
[*class]->[object]
In other words, what is actually being done in memory. Does array[0] = new class()
just return a reference to a reserved location in memory, and the class[] array = new class[10]
statement create something along the lines of 10 pointers, which are later assigned to by the new statements?
换句话说,就是在内存中实际执行的操作。是否array[0] = new class()
只是返回对内存中保留位置的引用,并且该class[] array = new class[10]
语句创建了 10 个指针的行,然后由新语句分配给这些指针?
回答by Joachim Sauer
Arrays in Java store one of two things: either primitive values (int
, char
, ...) or references (a.k.a pointers).
Java 中的数组存储两件事之一:原始值(int
, char
, ...)或引用(又名指针)。
So, new Integer[10]
creates space for 10 Integer
references only. It does notcreate 10 Integer
objects (or even free space for 10 Integer
objects).
因此,仅为new Integer[10]
10 个Integer
参考创建空间。它不会创建 10 个对象(甚至不会创建 10 个Integer
对象的可用空间Integer
)。
Incidentally that's exactly the same way that fields, variables and method/constructor parameters work: they too only store primitive values or references.
顺便说一句,这与字段、变量和方法/构造函数参数的工作方式完全相同:它们也只存储原始值或引用。
回答by helios
If you are familiar with C/C++ you can think of Java object references as pointers to objects(or pointers to structs). So:
如果您熟悉 C/C++,您可以将 Java 对象引用视为指向对象的指针(或指向结构的指针)。所以:
Person p = new Person();
p.setName("Helios");
is:
是:
- declare a p pointer to a Person struct (in the stack)
- reserve memory for and initialize Person struct
- assign its address to p
- execute method setName on object referenced by p
- 声明指向 Person 结构的 ap 指针(在堆栈中)
- 为 Person 结构保留内存并初始化
- 将其地址分配给 p
- 在 p 引用的对象上执行方法 setName
So when you are doing:
所以当你在做:
Person[] ps = new Person[5];
you are reserving an array of 5 references to Person. Next you will have to create each real person and assign each reference to a place in the array.
您正在保留对 Person 的 5 个引用的数组。接下来,您必须创建每个真实的人并将每个引用分配给数组中的一个位置。
Edit:the (almost) C/C++ version of the previous code
编辑:之前代码的(几乎)C/C++ 版本
class Person { ... };
typedef PersonStruct* Person; // I don't remember if this declaration is ok
Person p = new PersonStruct();
p -> setName(...);
Person[] ps = new Person[5];
// ps is a variable in the stack pointing to the array in the heap
// (being the array five references to the PersoStruct)
and you could do
你可以做
ps[3] = p;
回答by Manuel Salvadores
Arrays are continuous space of memory, so they look like more your first sketch:
数组是连续的内存空间,所以它们看起来更像是你的第一个草图:
[object-reference][object-reference]
[object-reference][object-reference]
array[0] = new class()
will store in array[0]
a reference to the new created object.
array[0] = new class()
将存储在array[0]
对新创建的对象的引用中。
class[] array = new class[10]
will create an array of ten empty slots (or ten null references).
class[] array = new class[10]
将创建一个包含十个空槽(或十个空引用)的数组。