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?

Kedar Mhaswade
  • 4,535
  • 2
  • 25
  • 34
Miranda
  • 129
  • 3
  • 12

5 Answers5

14

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.

MattPutnam
  • 2,927
  • 2
  • 17
  • 23
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.
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
4

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

loonytune
  • 1,775
  • 11
  • 22
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).

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
Aurasphere
  • 3,841
  • 12
  • 44
  • 71
-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.

  • 2
    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*. – Andy Turner Mar 29 '16 at 21:19
  • In case you are wondering, this is described in [JLS section 4.12.5](https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-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))." – Andy Turner Mar 29 '16 at 21:25
  • Thank you for the reference.. – Chaitanya Vyas Mar 29 '16 at 21:32