In c/c++, it is a must to specifiy a literal number of the array size when creating, e.g.:
int arr[10];
But in java, we can create an array in this form:
int size = 10;
int[] arr = new int[size];
And the compiler will not figure out what actually the variable size is. Then how does jvm allocate the memeories the array needs ?
Java allocates the array memory on the heap. The variable arr
is just a reference to the array, not the array itself. This is the same as allocating an array in c/c++ with malloc
or new
.