78

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

4
  • 14
    Throw away those textbooks. At worst they should've told you to make a call to Arrays.fill(myArr, 0). But it's still a ridiculous suggestion, really if it's a question of clarity put a damned comment in, don't do needless work. Commented Oct 8, 2010 at 19:58
  • 6
    The Java language guarantees that variables are initialized to a default value (false, 0, null, depending on the type). There's no need to manually initialize an array, it's just a waste of CPU cycles. Commented Oct 8, 2010 at 20:39
  • 1
    Those textbooks were probably written by C/C++ programmers, for which newly allocated arrays have undefined content. Java tries hard to avoid undefined behaviour. Commented Sep 7, 2013 at 9:50
  • This is what happens when C/C++ teachers are assigned to teach a Java class by the department chair without any additional training, the author may even have been a student of such a teacher... and here we are addressing antiquated assumptions. Commented Mar 1, 2017 at 18:07

4 Answers 4

139

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.
Sign up to request clarification or add additional context in comments.

1 Comment

can you treat the null character as a zero? calculation wise?
17

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

3 Comments

+1 and for the record, here's the official spec: Initial Values of Variables
This is right for array values and fields, but not local variables. The compiler requires you to initialize those yourself.
boolean is initialized to false, not zero.
7

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

Comments

4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.