Arrays in Java and how they are stored in memory

后端 未结 3 1223
情话喂你
情话喂你 2020-11-29 08:56

I\'m trying to understand the array setup in java. Why must you initialize space for each each object in the array, after you have created the array. How is it stored in mem

3条回答
  •  独厮守ぢ
    2020-11-29 09:28

    Arrays in Java store one of two things: either primitive values (int, char, ...) or references (a.k.a pointers).

    So, new Integer[10] creates space for 10 Integer references only. It does not create 10 Integer objects (or even free space for 10 Integer objects).

    Incidentally that's exactly the same way that fields, variables and method/constructor parameters work: they too only store primitive values or references.

提交回复
热议问题