java中数组保存在内存中的什么位置?

发布于 2024-11-29 01:59:35 字数 293 浏览 1 评论 0原文

如果我有一个函数,在该函数中我声明:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撕心裂肺的伤痛 2024-12-06 01:59:35

内存图:

内存图

框是内存位置(可以存储二进制数)。
箭头是内存引用(即指针)。

Memory diagram:

Memory diagram

Boxes are memory locations (where binary numbers can be stored).
Arrows are memory references (i.e. pointers).

我不是你的备胎 2024-12-06 01:59:35

理论上,堆栈有一个指针,指向堆中包含数组本身的位置。数组本身只是一个指针数组,它也指向堆中包含您引用的对象的位置。

在 Java 中,您几乎可以相信,每当您说 new ... 时,就会在堆中创建空间。一般来说,每当声明一个变量时,编译器都会在方法的上下文中为该变量保留堆栈空间。对于本机类型,该空间将保存表示值的实际字节。对于对象和数组,该变量将保存内存引用。

因此,例如,以下对象在堆中为其分配了单独的内存位置:

new Object[20]
new String("abc")
new List() // This contains a reference to an initial array, which is also on the heap.

请注意,很少有情况 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:

new Object[20]
new String("abc")
new List() // This contains a reference to an initial array, which is also on 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.

百善笑为先 2024-12-06 01:59:35

在 Java 中,基本上每次使用 new 关键字时,您都会在堆上分配空间来存储对象。

用于指向该对象的变量包含存储在堆栈上的引用。

例如:

                                    // This array object is
                                    // stored on the heap.
String[] arr                      = new String[5];
// This reference (arr) is stored
// in a variable on the stack.

对于像 Object[] 这样的引用类型数组,分配的空间是一个足够大的连续块,足以存储数组将要存储的许多引用抓住。任何特定的引用,例如 arr[0] 处的引用,本身都将指向堆上存储单个对象的另一个位置。

The array, somewhere on the heap:
[a*][b*][  ][  ][  ]

a (elsewhere on the heap):
"abc"

b (yet another heap location):
[A List object]

唯一的例外是基元数组,例如 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:

                                    // This array object is
                                    // stored on the heap.
String[] arr                      = new String[5];
// This reference (arr) is stored
// in a variable on the stack.

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 at arr[0], will itself point to another location on the heap where the individual object is stored.

The array, somewhere on the heap:
[a*][b*][  ][  ][  ]

a (elsewhere on the heap):
"abc"

b (yet another heap location):
[A List object]

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.

戈亓 2024-12-06 01:59:35

n 个字符串数组是

  • 堆上 n 个对象引用的后续列表
  • n 个单个 String 对象,以及对堆上
  • n 数组的引用> 堆上的字符数组

这个数组也有一个引用,可以保存在堆栈中或(作为类上的字段)保存在堆中

An Array of n Strings is

  • A subsequent list of n object references on the heap
  • n single String objects with a reference to an array on the heap
  • n arrays of chars on the heap

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文