Your code:
int arr1[4] = new int[];
will not compile. It should be:
int arr1[] = new int[4];
putting [] before the array name is considered good practice, so you should do:
int[] arr1 = new int[4];
in general, an array is created as:
type[] arrayName = new type[size];
The part above [size] determines the size of the selected array.
And why do we use new when creating an array?
Because arrays in Java are objects. The name of the array arrayName in the above example is not an actual array, but just a reference. The new operator creates an array on the heap and returns a reference to the newly created array object, which is then assigned to arrayName .
source share