java中数组保存在内存中的什么位置?
如果我有一个函数,在该函数中我声明:
Object arr[] = new Object[20];
arr 和整个数组存储在哪里?堆?堆? 声明是在某个函数中还是在 main() 中,这有关系吗?
假设我也有这些命令行:
arr[0] = new String("abc");
arr[1] = new List();
arr[0]
和 arr[1]
存储在哪里?
If I have a function that in that function I declare:
Object arr[] = new Object[20];
Where are arr and the whole array stored? heap? stack?
Does it matter if the declaration is in some function or in main()?
and let's say I also have these command lines:
arr[0] = new String("abc");
arr[1] = new List();
where are arr[0]
and arr[1]
stored?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
内存图:
框是内存位置(可以存储二进制数)。
箭头是内存引用(即指针)。
Memory diagram:
Boxes are memory locations (where binary numbers can be stored).
Arrows are memory references (i.e. pointers).
理论上,堆栈有一个指针,指向堆中包含数组本身的位置。数组本身只是一个指针数组,它也指向堆中包含您引用的对象的位置。
在 Java 中,您几乎可以相信,每当您说
new ...
时,就会在堆中创建空间。一般来说,每当声明一个变量时,编译器都会在方法的上下文中为该变量保留堆栈空间。对于本机类型,该空间将保存表示值的实际字节。对于对象和数组,该变量将保存内存引用。因此,例如,以下对象在堆中为其分配了单独的内存位置:
请注意,很少有情况
new String("abc")
优于"abc"
,因为字符串文字无论如何都会存在于包的内存中,并且字符串是不可变的。为内存中已存在的字符串的精确副本分配额外的内存是没有意义的。实际上,唯一需要注意的是编译器根本不必将局部变量存储在堆栈上。如果确定变量的范围足够短,则可以自由地优化堆栈引用并仅使用寄存器。
In theory, the stack has a single pointer to a location in the heap that contains the array itself. The array itself is just an array of pointers which also point to locations in the heap that contain the objects you reference.
In Java, you can pretty much count on the fact that any time you say
new ...
, space is being created in the heap. Generally speaking, any time you declare a variable, the compiler will reserve stack space in the method's context for that variable. For native types, that space will hold the actual bytes to represent the value. For objects and arrays, that variable will hold a memory reference.So, for example, the following objects have separate memory locations allocated for them in the heap:
Note that there are very few times when
new String("abc")
is preferable to"abc"
, since string literals are going to exist in the package's memory anyway, and strings are immutable. There's no point allocating extra memory for an exact copy of a string that exists in memory already.In practice, the only caveat is that the compiler doesn't necessarily have to store local variables on the stack at all. If it determines that the scope of the variable is short enough, it's free to optimize away the stack reference and just use a register for it.
在 Java 中,基本上每次使用 new 关键字时,您都会在堆上分配空间来存储对象。
用于指向该对象的变量包含存储在堆栈上的引用。
例如:
对于像
Object[]
这样的引用类型数组,分配的空间是一个足够大的连续块,足以存储数组将要存储的许多引用抓住。任何特定的引用,例如 arr[0] 处的引用,本身都将指向堆上存储单个对象的另一个位置。唯一的例外是基元数组,例如 int[]:在这种情况下,数组本身仍然是堆上的连续块,但数组中的每个位置都包含实际值本身而不是对堆上另一个位置的引用。
In Java, basically any time you use the
new
keyword you are allocating space on the heap for an object to be stored.The variable you use to point to that object contains a reference stored on the stack.
So for example:
In the case of an array of reference types like an
Object[]
, the space allocated is a contiguous block large enough to store however many references the array will hold. Any particular reference, such as the one atarr[0]
, will itself point to another location on the heap where the individual object is stored.The only exception to this is with arrays of primitives, like
int[]
: in this case the array itself is still a contiguous block on the heap, but each position within the array contains the actual value itself rather than a reference to another position on the heap.n 个字符串数组是
这个数组也有一个引用,可以保存在堆栈中或(作为类上的字段)保存在堆中
An Array of n Strings is
This Array then has a reference as well that can be held on the stack or (as a field on a class) in the heap