But in Java, not arrays of objects, but only arrays of links? That is, each object in the array, as well as the array object itself, is separated in memory and treated as a separate garbage collector?
Yes.
I think there is confusion because AFAIK, in C:
- It is possible to have an array of structures that really store structures.
- It is also possible to have an array of pointers to structures.
I'm sure Java always uses 1. for arrays of primitive types and always uses 2. for arrays of objects, and C can use either for any type ...?
Java, like C, usually stores arrays of primitive types as real arrays with elements of these types. Thus, an int[] array with 10 elements usually reserves 10 × 4 bytes for the array, plus the overhead for the entire array object.
Arrays of objects, however, are, as you say, arrays of links. Thus, an object[] of 10 elements will typically occupy 10 × 4 bytes (or perhaps 10 × 8 bytes in 64-bit CPUs) for the array, plus overhead, plus space for each object that refers to each non-zero element , This corresponds to a C array of pointers.
(I use the term “usually” because although, as most JVMs do, they are not required to allocate memory in any special way.)
Also keep in mind that Java does not have true multidimensional arrays like C (or C #). int[][] in Java is actually a one-dimensional array, where each element is a link to its own int[] subframe. In C a int[][] indeed a two-dimensional array of integers (where the lengths of all but the first dimension must be known at compile time).
Adding
Also note that, as you say, C can have true arrays of structures that are neither primitive types nor pointers. Java does not have this feature.