where will an array declared like this int[][][] arr = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } }; in java get stored?

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

where will an array declared like this int[][][] arr = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } }; in java get stored?

问题

一般情况下,在Java中,使用 "new" 关键字来创建数组,但这也是另一种方法,所以我很好奇这个数组会被存储在Java中的哪里,它会在堆栈还是堆内存中存储?

英文:

generally, in java new keyword is used to make an array but this is also a way to do it so I am curious where would this array be stored in java would it be in a stack or in heap memory

答案1

得分: 4

如评论中所解释:

int[][][] arr = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };

是以下代码的语法糖:

int[][][] arr = new int[][][] { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };

在两种情况下,变量 arr 被分配在堆栈上(如果它是局部变量);但实际的数组对象,即 arr 引用的对象,是在堆上分配的。

英文:

As explained in the comments:

int[][][] arr = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };

Is syntactic sugar for

int[][][] arr = new int[][][] { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };

In both cases, the variable arr is allocated on the stack (if it is a local variable); but the actual array object which arr references is allocated on the heap.

答案2

得分: 1

数组对象始终存储在堆内存中。

英文:

Array object always store in the heap memory.

答案3

得分: 0

数组是一个对象,因此

变量 + 指针放入堆栈

实际值放入堆中

英文:

Array is an object so

Variable + pointer goes in stack

Actual value goes in heap

huangapple
  • 本文由 发表于 2020年8月25日 18:39:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63577097.html
匿名

发表评论

匿名网友

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

确定