java:原始数组——它们是否已初始化? [英] java: primitive arrays -- are they initialized?
本文介绍了java:原始数组——它们是否已初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我在代码中使用像
int[] a = new int[42];
它会将数组初始化为任何特别的东西吗?(例如 0)我似乎记得在某处记录了这一点,但我不确定要搜索什么.
Will it initialize the array to anything in particular? (e.g. 0) I seem to remember this is documented somewhere but I am not sure what to search for.
推荐答案
在 15.10 数组创建表达式 JLS 说
[...] 创建指定长度的一维数组,并将数组的每个组件初始化为其默认值
[...] a single-dimensional array is created of the specified length, and each component of the array is initialized to its default value
以及 4.12.5 变量的初始值 它说:
对于类型int
,默认值为0,即0
.
For type
int
, the default value is zero, that is,0
.
这篇关于java:原始数组——它们是否已初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文