我们可以假设 Java 中的默认数组值吗?例如,假设一个 int 数组设置为全零?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3893663/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:28:20  来源:igfitidea点击:

Can we assume default array values in Java? for example, assume that an int array is set to all zeros?

java

提问by Jose M Vidal

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?

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

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

这适用于所有类型吗?字符?布尔值?枚举?

Where is this officially documented?

这是哪里有官方记录?

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".

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

采纳答案by Nikita Rybak

Java Language Specificationis the right place to look for such information:

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

Array componentsare 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 节)

Default values themselves are given in section 4.12.5.

默认值本身在第 4.12.5 节中给出

  • 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.
  • 对于byte类型,默认值为零,即(byte)0的值。
  • 对于short类型,默认值为零,即(short)0的值。
  • 对于int类型,默认值为零,即0
  • 对于long类型,默认值为零,即0L
  • 对于float类型,默认值为正零,即0.0f
  • 对于double类型,默认值为正零,即0.0d
  • 对于char类型,默认值为空字符,即'\u0000'
  • 对于类型boolean,默认值为false
  • 对于所有引用类型,默认值为null

回答by Etienne de Martel

Yes. Primitive types in Java are always zero-initialized. References are also initialized to null.

是的。Java 中的原始类型总是零初始化的。引用也初始化为空。

回答by TofuBeer

Your textbook is wrong! Writing such code is pointless, and a waste of your time typing it and the computers time running it.

你的课本错了!编写这样的代码是没有意义的,而且浪费了你输入它的时间和运行它的计算机时间。

As others have said, the compiler guarantees that variables outside of methods (class/instance variables) are given a value of 0, false, or null. As you probably know the compiler does not give variables inside of methods values, and instead forces you to give them a value before they are used.

正如其他人所说,编译器保证方法之外的变量(类/实例变量)的值为 0、false 或 null。您可能知道,编译器不会在方法值中提供变量,而是强制您在使用它们之前为其提供值。

You will find, if you do things "right" that about 90%-95% of your "variables" never change after they are given a value. However, new programmers tend to do things like this:

你会发现,如果你做“正确”的事情,那么大约 90%-95% 的“变量”在给定值后永远不会改变。然而,新程序员倾向于做这样的事情:

int x = 0;

// some code that never uses x

x = 5;

// more code that only reads x and never modifies it.

this prevents you from being able to mark x as "final". If you are able to mark x as "final" then it prevents accidental modification of the value, which prevents bugs.

这会阻止您将 x 标记为“最终”。如果您能够将 x 标记为“最终”,那么它可以防止意外修改该值,从而防止出现错误。

I would write the code like:

我会写这样的代码:

final int x;

// some code that never uses x

x = 5;

// more code that only reads x and never modifies it.

This is called a blank final (a final variable that doesn't have a value when it is declared).

这称为空白 final(声明时没有值的 final 变量)。

If you remember that class/instance variables are initialized by the runtime then you will, hopefully, not write code to initialize them with throw away values, and then you can mark them as final.

如果您记得类/实例变量是由运行时初始化的,那么您希望不会编写代码来使用丢弃值初始化它们,然后您可以将它们标记为最终的。

My personal practice is to always mark everything as final until I find that I need to modify it, and to never initialize a variable until I know the actual value I want it to have. Doing this make the code faster (not noticeable since assignments are generally very quick) and safer since I rarely accidentally modify a value when I should not.

我个人的做法是始终将所有内容标记为 final,直到我发现需要修改它为止,并且永远不要初始化变量,直到我知道我希望它具有的实际值。这样做可以使代码更快(不明显,因为赋值通常非常快)并且更安全,因为我很少在不应该修改的时候意外修改一个值。

回答by user1804314

It should be Java Language Specification §4.12.5 Initial Values of Variables. instead of §4.5.5

它应该是 Java Language Specification §4.12.5 Initial Values of Variables。而不是§4.5.5

"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 (§4.3), the default value is null."

"对于byte类型,默认值为0,即(byte)0的值。
对于short类型,默认值为0,即(short)0的值。
对于int类型,默认值为零,即0。
对于long类型,默认值为0,即0L。
对于float类型,默认值为正零,即0.0f。
对于double类型,默认值为正零,即0.0d。
对于char类型,默认值为空字符,即'\u0000'。
对于boolean类型,默认值为false。
对于所有引用类型(§4.3),默认值为空值。”