How array works internally in Java?

后端 未结 1 702
日久生厌
日久生厌 2021-01-13 08:00

This query is posted to basically understand points like

  • An object is class instance or an array;

  • An array is a subclass of Object

相关标签:
1条回答
  • 2021-01-13 08:04

    To answer your question: yes, in Java (and C#), nearly everything is split into multiple discrete chunks of memory accessed by pointers. This not only include your bidimensional array but also any embedded object inside your object C. In C++, if you have an array (single dimensional or not) of 10 objects and each of these object contains 10 embedded objects, you can allocate all this with a single piece of memory. In C# and Java, you will have a minimum of 101 memory allocations for storing all this (if all of the embedded objects are simple objects) in the case of a mono-dimensional array and more in the case of a multi-dimensional array.

    However, this explosion of pieces of memory shouldn't be seen a something very bad because it's free you of the difficulty of managing yourself the allocation of memory as you can have with C/C++ and in most cases, the power of any modern CPU is generally sufficient to pull it forward at a sufficient speed.

    0 讨论(0)
提交回复
热议问题