Java 空数组和空数组有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27476845/
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
What is the difference between a null array and an empty array?
提问by
If the individual elements of an int array are not initialized, what is stored in them by default? I apparently found that there is something like an empty array or a null array. What is the difference, and which one applies to my first question?
如果 int 数组的各个元素未初始化,默认情况下它们存储了什么?我显然发现有类似空数组或空数组的东西。有什么区别,哪个适用于我的第一个问题?
采纳答案by ruakh
Technically speaking, there's no such thing as a null array; but since arrays are objects, array types are reference types (that is: array variables just hold referencesto arrays), and this means that an array variable can be null
rather than actually pointing to an array:
从技术上讲,没有空数组这样的东西;但由于数组是对象,数组类型是引用类型(即:数组变量只保存对数组的引用),这意味着数组变量可以null
而不是实际指向数组:
int[] notAnArray = null;
An emptyarray is an array of length zero; it has no elements:
一个空数组是零长度的阵列; 它没有元素:
int[] emptyArray = new int[0];
(and can neverhave elements, because an array's length never changes after it's created).
(并且永远不能有元素,因为数组的长度在创建后永远不会改变)。
When you create a non-empty array without specifying values for its elements, they default to zero-like values — 0
for an integer array, null
for an array of an object type, etc.; so, this:
当你创建一个非空数组而不为其元素指定值时,它们默认为类似零的值——0
对于整数数组、null
对象类型的数组等;所以这:
int[] arrayOfThreeZeroes = new int[3];
is the same as this:
与此相同:
int[] arrayOfThreeZeroes = { 0, 0, 0 };
(though these values can be re-assigned afterward; the array's length cannot change, but its elements can change).
(虽然这些值可以在之后重新分配;数组的长度不能改变,但它的元素可以改变)。
回答by Jigar Joshi
If the individual elements of an int array are not initialized, what is stored in them by default?
如果 int 数组的各个元素未初始化,默认情况下它们存储了什么?
0
0
empty array is array with 0 elements
空数组是具有 0 个元素的数组
I haven't heard about null
array but it is probably an array with non zero element reference which are null
我还没有听说过null
数组,但它可能是一个具有非零元素引用的数组,它们是null
回答by Jigar
By default java initialized the array according to type declared. It is int then it is initialized to 0. If it is of type object like array of object then it is initialized to null.
默认情况下,java 根据声明的类型初始化数组。它是 int 然后它被初始化为 0。如果它是类型对象,如对象数组,那么它被初始化为 null。
回答by Elliott Frisch
An array has it's members initialized to their default values. For int
the default is 0. For an Object
it's null
. A null
array is a null
Array Reference(since arrays are reference types in Java).
数组的成员被初始化为默认值。对于int
默认值是 0。对于Object
它是null
. 甲null
阵列是null
阵列参考(因为数组是引用类型在Java中)。
JLS-4.12.5 Initial Values of Variablessays in part
For type int, the default value is zero, that is, 0.
对于 int 类型,默认值为 0,即 0。
and
和
For all reference types (§4.3), the default value is null.
对于所有引用类型(第 4.3 节),默认值为 null。