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

Java

Java Problem Overview


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 Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

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

Solution 4 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJose M VidalView Question on Stackoverflow
Solution 1 - JavaNikita RybakView Answer on Stackoverflow
Solution 2 - JavaEtienne de MartelView Answer on Stackoverflow
Solution 3 - Javauser1804314View Answer on Stackoverflow
Solution 4 - JavaTofuBeerView Answer on Stackoverflow