javaarraysnullinitializationzero

What value is stored in an initialized but empty integer array in Java


I made the following array:

int[] array = new int[9];

This array is initialized, but empty.

In that case, what is the value of any index of this array?

When I print it out, it gives me 0.
Does that mean there is a zero stored in here (like if you had called int x = 0)?
Or is it null?

Also, is this the same for any Object array?
Is it an empty instance of this object, or is it null?


Solution

  • If it is an Array of objects it will be initialized with null, if it's an array of primitive values (like int) it will be initialized with 0. For the non-number primitive boolean it is false and for 'char' it is '\u0000'. For more information, check the table Default Values on the following page:

    http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html