If I use a statement in my code like
int[] a = new int[42];
Will it initialize the array to anything in particular? (e.g. 0) I seem to remember this is documented somewhere but I am not sure what to search for.
If I use a statement in my code like
int[] a = new int[42];
Will it initialize the array to anything in particular? (e.g. 0) I seem to remember this is documented somewhere but I am not sure what to search for.
The array would be initialized with 42 0s
For other data types it would be initialized with the default value ie.
And so on.
For objects in general it would be null:
At 15.10 Array Creation Expressions the JLS says
and at 4.12.5 Initial Values of Variables it says:
All elements in the array are initialized to zero. I haven't been able to find evidence of that in the Java documentation but I just ran this to confirm:
When created, arrays are automatically initialized with the default value of their type - in your case that would be
0
. The default isfalse
forboolean
andnull
for all reference types.