Is there any difference between the following two declarations?
int arr[] = new int [5];
and
int arr1[] = {1,2,3,4,5};
The first line puts one new object on the heap -an array object holding four elements- with each element containing an int with default value of 0.
The second does the same, but initializing with non default values. Going deeper, this single line does four things:
If you use an array of objects instead of primitives:
MyObject[] myArray = new MyObject[3];
then you have one array object on the heap, with three null references of type MyObject, but you don't have any MyObject objects. The next step is to create some MyObject objects and assign them to index positions in the array referenced by myArray.
myArray[0]=new MyObject();
myArray[1]=new MyObject();
myArray[2]=new MyObject();
In conclusion: arrays must always be given a size at the time they are constructed. The JVM needs the size to allocate the appropriate space on the heap for the new array object.