我们可以假设Java中的默认数组值吗?假设一个int数组被设置为全零? [英] Can we assume default array values in Java? for example, assume that an int array is set to all zeros?

查看:408
本文介绍了我们可以假设Java中的默认数组值吗?假设一个int数组被设置为全零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实践中我可以假设Java中的所有int数组都会以零填充吗?对于运行JVM的所有机器?

In practice can I assume that all int arrays in Java will start out filled with zeros? for all machines in which the JVM runs?

这对所有类型都适用吗?炭?布尔?枚举?

Is this true for all types? char? boolean? enums?

这在哪里正式记录?

教科书我说int数组设置为零但他们也建议人们应该写一个for循环来将所有值设置为零只是更清楚。

The textbooks I have say that int arrays are set to zero but they also recommend that one should write a for-loop to set all values to zero just "to be clearer".

推荐答案

Java语言规范是查找此类信息的正确位置:

Java Language Specification is the right place to look for such information:


数组组件是未命名的变量,已创建并初始化为默认值(§4.12.5)每当创建一个数组的新对象时

Array components are unnamed variables that are created and initialized to default values (§4.12.5) whenever a new object that is an array is created

默认值本身在第4.12.5节



  • 对于类型 byte ,默认值为零,即值的(byte)0

  • 对于 short 类型,默认值为零,即的值(short )0

  • 对于 int 类型,默认值为零,即 0

  • 对于 long 类型,默认值为零,即 0L

  • 对于类型浮动,默认值为正零,即 0.0f

  • 类型 double ,默认值为正零,即 0.0d

  • 对于类型 char ,默认值为空字符,即'\ u0000'

  • 对于类型布尔,默认值为 false

  • 对于所有参考类型,默认值为 null

  • For type byte, the default value is zero, that is, the value of (byte)0.
  • For type short, the default value is zero, that is, the value of (short)0.
  • For type int, the default value is zero, that is, 0.
  • For type long, the default value is zero, that is, 0L.
  • For type float, the default value is positive zero, that is, 0.0f.
  • For type double, the default value is positive zero, that is, 0.0d.
  • For type char, the default value is the null character, that is, '\u0000'.
  • For type boolean, the default value is false.
  • For all reference types, the default value is null.

这篇关于我们可以假设Java中的默认数组值吗?假设一个int数组被设置为全零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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