在 Java 中不使用“new”关键字声明数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39032577/
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
Declaring arrays without using the 'new' keyword in Java
提问by Muhammad Ali Qadri
Is there any difference between the following two declarations?
以下两个声明有什么区别吗?
int arr[] = new int [5];
and
和
int arr1[] = {1,2,3,4,5};
Is arr1
declared on stack or on the heap?
是arr1
在堆栈上还是在堆上声明?
回答by Andy Turner
There is the obvious difference that one has all zeros, and the other contains [1..5].
一个明显的区别是一个全为零,另一个包含 [1..5]。
But that's the only difference. Both are 5-element int arrays, both are allocated in the same way. It is mere syntactic convenience to declare with the braces and no new
.
但这是唯一的区别。两者都是 5 个元素的 int 数组,它们的分配方式相同。用大括号和 no 声明只是语法上的方便new
。
Note that this form can only be used when the array is declared:
请注意,这种形式只能在声明数组时使用:
int[] blah = {}
But not
但不是
int[] blah;
blah = {};
or
或者
return {};
Objects (arrays are objects) are allocated on the heap.
对象(数组就是对象)在堆上分配。
回答by debus
The first line puts one new object on the heap -an array object holding four elements- with each element containing an int with default value of 0.
第一行将一个新对象放在堆上——一个包含四个元素的数组对象——每个元素包含一个默认值为 0 的 int。
The second does the same, but initializing with non default values. Going deeper, this single line does four things:
第二个执行相同的操作,但使用非默认值进行初始化。再深入一点,这行代码做了四件事:
- Declares an int array reference variable named arr1
- Creates an int array with a length of five (five elements).
- Populates the array's elements with the values 1,2,3,4,5
- Assigns the new array object to the reference variable arr1
- 声明一个名为 arr1 的 int 数组引用变量
- 创建一个长度为五(五个元素)的 int 数组。
- 用值 1,2,3,4,5 填充数组的元素
- 将新数组对象分配给引用变量 arr1
If you use an array of objects instead of primitives:
如果您使用对象数组而不是基元:
MyObject[] myArray = new MyObject[3];
then you have one array object on the heap, with three null references of type MyObject, but you don't have any MyObject objects. The next step is to create some MyObject objects and assign them to index positions in the array referenced by myArray.
那么你在堆上有一个数组对象,有三个 MyObject 类型的空引用,但你没有任何 MyObject 对象。下一步是创建一些 MyObject 对象并将它们分配给 myArray 引用的数组中的索引位置。
myArray[0]=new MyObject();
myArray[1]=new MyObject();
myArray[2]=new MyObject();
In conclusion: arrays must always be given a size at the time they are constructed. The JVM needs the size to allocate the appropriate space on the heap for the new array object.
总之:数组在构造时必须始终被赋予一个大小。JVM 需要在堆上为新数组对象分配适当空间的大小。
回答by Ole V.V.
I agree with the other answers, by far the most often you array will be allocated on the heap (no matter which of the two declarations you use). However, according to the top answer in Can Java allocate a list on stack?, “in special cases, the java virtual machine may perform escape analysis and decide to allocate objects … on a stack”. I believe that this is true. So the answer to your question is: It depends. Usually on the heap.
我同意其他答案,到目前为止,您最常在堆上分配数组(无论您使用两个声明中的哪一个)。但是,根据Can Java allocation a list on stack? ,“在特殊情况下,java 虚拟机可能会执行逃逸分析并决定在堆栈上分配对象……”。我相信这是真的。所以你的问题的答案是:这取决于。通常在堆上。
回答by frogatto
new int [5]
can be used for both assignmentand initialization, but{1, 2}
only can be used as declaration with initialization. (Note thatnew int[] {1, 2}
also can be used as both assignmentand initialization)new int [5]
sets all entries to zero, but{1, 2}
andnew int[] {1, 2}
sets1
and2
in respective entries.Both are on heap, you can save their object reference.
int arr[] = new int [5]; // arr: object reference to the array
or
int arr[] = {1, 2, 3, 4, 5}; // arr: object reference to the array
new int [5]
可用于赋值和初始化,但{1, 2}
只能用作带初始化的声明。(注意new int[] {1, 2}
也可以同时用作赋值和初始化)new int [5]
将所有条目为零,但{1, 2}
并new int[] {1, 2}
集1
和2
在相应条目。两者都在堆上,您可以保存它们的对象引用。
int arr[] = new int [5]; // arr: object reference to the array
或者
int arr[] = {1, 2, 3, 4, 5}; // arr: object reference to the array
Helpful materials:
有用的材料: