Can we assume default array values in Java? for ex

2020-01-27 00:35发布

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?

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

标签: java
4条回答
SAY GOODBYE
2楼-- · 2020-01-27 00:54

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

查看更多
▲ chillily
3楼-- · 2020-01-27 00:58

It should be Java Language Specification §4.12.5 Initial Values of Variables. instead of §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."

查看更多
Emotional °昔
4楼-- · 2020-01-27 01:06

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.

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:

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.

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

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.

查看更多
Viruses.
5楼-- · 2020-01-27 01:12

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

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

Default values themselves are given in section 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.
查看更多
登录 后发表回答