If the individual elements of an int array are not initialized, what is stored in them by default? I apparently found that there is something like an empty array or a null array. What is the difference, and which one applies to my first question?
Java – the difference between a null array and an empty array
arraysjava
Best Answer
Technically speaking, there's no such thing as a null array; but since arrays are objects, array types are reference types (that is: array variables just hold references to arrays), and this means that an array variable can be
null
rather than actually pointing to an array:An empty array is an array of length zero; it has no elements:
(and can never have elements, because an array's length never changes after it's created).
When you create a non-empty array without specifying values for its elements, they default to zero-like values —
0
for an integer array,null
for an array of an object type, etc.; so, this:is the same as this:
(though these values can be re-assigned afterward; the array's length cannot change, but its elements can change).