Memory Allocation for Arrays of Objects

In my computer science course, we were taught that when creating an array, the JVM automatically allocates memory depending on the size of the array. For example, if you create an integer array of size 10, the JVM will allocate 10 * 32 bits of data to this array.

My question is, how exactly does this process work when you create object arrays with different sizes? For example, a String object. When you create an array of 10 lines, is there any memory reserved in the system for these lines, or since they are just pointers, no memory allocation is required?

+4
source share
3 answers

String - , Object, Java ( ) , String. ,

String[] a = new String[10];

, ( , ) (32 32- 64 64- ).

:, , .

+3

int[] = > ints

String [] = > String

int[][] = > (, ) int []

+2

Java, . :

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

// create an array of integers

anArray = new int [10];

If this is the case, then the compiler prints an error, for example, the following and compilation fails:

ArrayDemo.java-00-00: the anArray variable may not have been initialized.

Another fooobar.com/questions/53717 / ... .

0
source

All Articles