how does jvm allocate memroies when creating array with non literal size

L.Maple :

In c/c++, it is a must to specifiy a literal number of the array size when creating, i.e. 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 ?

Henry :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=89635&siteId=1