4

I noticed that if I declare an array as:

int[] myarr = new int[10];

I can directly use myarr[1] ++; so that myarr[1] = 1. Does it mean that in Java, we do not need to initialize the array and set each value as 0 by the following method?

for (int i = 0; i < myarr.length; i++) {
     myarr[i] = 0;
}

I saw from some comments that the array may contain garbage values. If the array is an integer array, will it contain any garbage other than 0?

1
  • Do you need to initialize anything in Java? Commented Apr 4, 2016 at 6:32

5 Answers 5

15

In Java, all array elements are automatically initialized to the default value. For primitive numerical types, that's 0 or 0.0. For booleans, that's false. For objects, that's null.

In other languages such as C++, the values in an uninitialized array are undefined. Some compilers may initialize to 0/null similarly for security, and it's very bad practice to rely on this. However, this behavior is well defined in Java and so it's perfectly okay to create a primitive array and trust that the values are 0.

Sign up to request clarification or add additional context in comments.

2 Comments

6

A newly-initialized int[] will be filled with zeros, by language specification.

Referring to JLS §10.6 (Array Initializers):

A one-dimensional array is created of the specified length, and each component of the array is initialized to its default value (§4.12.5).

Referring to §4.12.5:

  • Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10.2):
    • 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

no, all java arrays are filled with the appropriates type default value (0 for ints, 0.0 for doubles, null for references, ...)

Comments

0

You can't skip the array initialization but you don't have to initialize each element of the array. If you don't initialize an element in an array it works exactly like it would if you weren't initializing a member variable of that specific type: Java will initialize it with the type default value which is 0 for numerical primitive types (int, double, float...), false for booleans and null for objects (String included).

Comments

-3

No, it is compulsory to initialize an array in java.. When I was writing my programs, I didn't initialize my array and my compiler (jdk7) gave me an error like:

ArrayOne.java:4: Variable myArray may not have been initialized.

Thus failing my compilation.

3 Comments

That's not the same thing: it's saying that you never initialized the array variable, not its components. It'd do the same thing for non-array variables too. This requirement is called definite assignment.
In case you are wondering, this is described in JLS section 4.12.5: "A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified using the rules for definite assignment (§16 (Definite Assignment))."
Thank you for the reference..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.