It's important to know differences between declaring vs allocating an array, and initialization vs assigning values to it.
Declaring an array is stating its dimensions, also creating a variable that can hold a reference to one.
The array object itself is allocated (memory carved out for it and its elements, and its construction is run) when the new
keyword is used because array is a reference type.
If you skip new
then array variable itself contains null (which differs from an empty array having 0 elements).
When array is allocated, all elements receive the default value of the array's data type - so 0 for all elements of an int[ ] and numeric types, null for object[ ] and other reference types, false for bool[ ], etc.
However if you { initialize an array using braces }, you put values directly into the elements and skip them receiving the default value. So initialization will occur explicitly if you provide the values otherwise implicitly if they are allowed to default.
Making a loop later (or something different than a loop) and populating the array with data is a second pass of assigning values to its elements (after initialization).
Sometimes the term "defining" an array is used to mean = declaration + allocation + initialization (It's been said this is not widely used in Java terminology)
// 2 elements having two string object refs
String s1[] = {"12","saf"};
// 2 elements holding 2 integer values
int s2[] = {1,2};
// 2 elements with no objects allocated (elements are null)
Object s3[] = new Object[2];
// 3 string objects
String s4[][] = {{"12","2d"},{"saf"}};
// 4 string objects
String s5[][] = {{"12","2d"},{"saf","fds"}};
/* declared array holds 2 arrays each can contain 2 objects
but they're not allocated so nested array elements are all null
*/
Object s6[] = {new Object[2],new Object[2]};