空数组自动初始化内容是什么? [英] Empty arrays automatically initialize contents?

查看:163
本文介绍了空数组自动初始化内容是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么

int alone;
System.out.println(alone);

给出错误,但

 int[] arr = new int[1];
 System.out.println(arr[0]);

等于0?
当你初始化一个空数组,它自动初始化其内容为0(或为空,等等)?

Equals 0? When you initialize an empty array, does it automatically initialize its contents to 0 (or null, etc.)?

推荐答案

从的语言标准

否则,一个一维数组,创建指定长度的,并且阵列的每个组件被初始化为默认值(第 4.12.5 )。

Otherwise, a one-dimensional array is created of the specified length, and each component of the array is initialized to its default value (§4.12.5).

和缺省值是:


  • 有关类型字节,默认值是零,也就是(字节)值0。

  • For type byte, the default value is zero, that is, the value of (byte)0.

有关型短,默认值是零,即,的值(短)0

For type short, the default value is zero, that is, the value of (short)0.

有关int类型,默认值是零,也就是0。

For type int, the default value is zero, that is, 0.

有关long类型,默认值是零,也就是0L。

For type long, the default value is zero, that is, 0L.

有关float类型,默认值是正零,也就是0.0。

For type float, the default value is positive zero, that is, 0.0f.

有关double类型,默认值是正零,也就是0.0D。

For type double, the default value is positive zero, that is, 0.0d.

有关char类型,默认值是空字符,也就是'\\ u0000的。

For type char, the default value is the null character, that is, '\u0000'.

有关布尔型,默认值是假的。

For type boolean, the default value is false.

有关所有引用类型(§ 4.3 ),默认值为null。

For all reference types (§4.3), the default value is null.

这篇关于空数组自动初始化内容是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆