6

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?

6
  • You actually build a pointer array, that's correct. So you need the sizeof(pointer) * array_size. Instancing a object you gonna link to an array index takes care about the actual memory allocation of the object. Commented Nov 20, 2013 at 15:20
  • 2
    This question pretty much answers yours stackoverflow.com/questions/5564423/… Commented Nov 20, 2013 at 15:24
  • Actually, in Java, there is only one type of object that has a variable size: array. Everything else has a predetermined size (String holds an array internally). Commented Nov 20, 2013 at 16:41
  • Hi, There are a couple of misconceptions in your statement. 1) When you create an array like: 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. Commented Nov 23 at 17:24
  • 2) For primitive arrays, the data size is indeed proportional to the number of elements: element count × element size However, 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. Commented Nov 23 at 17:27

3 Answers 3

6

Since the String is a class which extends the Object class, and objects in Java are passed (and stored in variables) by reference, the array of strings is an array of references to String objects. So, when you do

String[] a = new String[10];

you're creating an array of references, where the size of every reference (not the object it's pointing to) is already known (32 bits for 32-bit machines, and 64 bits for 64 bits machines).

Upd: as Jon Skeet said in one of his answers the size of an actual reference may be the same as a native pointer size, but it's not guaranteed.

Sign up to request clarification or add additional context in comments.

Comments

4

int[] => array of ints

String [] => array of pointers to String instances

int[][] => array of pointers to (separate, disparate) int[] arrays

Comments

0

Arrays are itself an object in Java so it will be always created in runtime. From Official tutorial:

One way to create an array is with the new operator. The next statement in the ArrayDemo program allocates an array with enough memory for 10 integer elements and assigns the array to the anArray variable.

// create an array of integers

anArray = new int[10];

If this statement is missing, then the compiler prints an error like the following, and compilation fails:

ArrayDemo.java:4: Variable anArray may not have been initialized.

Also another answer in StackOverflow.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.