In my computer science course, we were taught that when you create an array, the JVM will allocate the memory automatically depending on the size of the array. For example if you create an integer array with a size of 10, the JVM would allocate 10 * 32 Bits of data to that array.
My question is, how exactly does this process work when you create arrays of object that have varying sizes? For example a String object. When you create an array of 10 Strings, is any memory actually reserved on the system for these strings, or since they are just pointers, memory allocation isn't necessary?
String[] arr = new String[10];The JVM allocates memory only for the array object itself, which contains 10 reference slots, all initialised to null. No String objects are created at this point. Actual String instances are allocated later, individually, when you explicitly create them.element count × element sizeHowever, this is not the total allocation. Java arrays are objects, so the JVM also includes: - an object header (typically 12–16 bytes), - a length field (4 bytes), - and padding for alignment (commonly to 8-byte boundaries). For example, an int[10] array contains: - 40 bytes of element data (10 × 4 bytes), - plus header, length, and alignment. In practice, the total allocation is typically around 56–64 bytes, depending on the JVM and heap configuration.