Array memory allocation
When you create an array of int's (not Integers), does java allocate an array of references for ints or the space for the ints themselves? I will be using 64-bit memory addresses for the example.
For example, if I create an int numbers = new int;
Did java do:
[64-bit array address]
[64-bit address][64-bit address][64-bit address][64-bit address]
The 64-bit address being a pointer to the memory address of the int.
[64-bit array address]
[32-bit int][32-bit int][32-bit int][32-bit int]
using the method of adding 32 bits to the first memory address to address
the remaining elements in the array?
Keep in mind that I am asking about an array of a primitive data type, I fully understand that allocation of memory for an array of objects happens like the first example.
I recently moved a program I was developing from a 32-bit system to a 64-bit system and drastically less objects than the theoretical maximum for the system could be created. If Java does indeed use the first method for allocation of primitive data types, when allocating an array of ints, that array will be one 33% larger moving from a 32-bit system to a 64-bit system, and 66% larger than is really necessary.
ints in Java are 32 bits no matter the platform.
r035198x, Thank you for your response, but that is not what I was asking at all. I am aware that ints are 32-bit across platforms. My question was pertaining to how an array of primitive data types are allocated in memory. As an array 64-bit pointers to the primitive data type or as an array of those actual data types in contiguous memory space.
I thought of a way to figure this out using Instrumentation. The answer is that Java does allocate arrays of primitive types using the second method I suggested.
If you knew that integers are 32 bits then you would have known the answer.
Especially if you know what happens when you do as opposed to when you do
Object a = new Object();
r035198x, Thank you for your follow up response. I appreciate the promptness of this forum to assist with a problem. r035198x, you have still missed the premise of the question. The question states that I am aware that integers are 32-bits. The question was querying how close Java sticks to the Object Oriented paradigm. In strict object orientation even an array of primitive types would be treated as an array of object pointers pointing to the primitive typed objects. So you will see that knowing that integers are 32-bits does not aid in answering the question. I apologize if my question was unclear in any way. If anyone can think of how I might improve clarity, I will gladly accept advice so that I might avoid confusion in the future. Thanks again for your time.
an int has a 32 bit or 64 bit reference depending on the 32 bit or 64 bit JVM. Each item in that array is a primitive of type int and thus each is 32 bits.
[reference size + other object overhead + 32 bits * length]
Read my last response. It's nothing much to do with arrays but to do with primitive types vs reference types. The variables of primitive types themselves hold the value while variables of reference types do not hold the object but point to the location of the object.
Originally Posted by number5isalive
And please stop posting stupid messages on my profile page.