I'm trying to wrap my head around the way that raw memory looks in different languages ββwhen using an array.
Consider the following Java code:
String a = "hi"; String b = "there"; String c = "everyone"; String[] array = {a, b, c};
Obviously, the array contains references, not objects; those. in memory there is a continuous array of three links, each of which points to a different place in memory where the object is located. Therefore, the objects themselves are not necessarily in three adjacent buckets; rather, there are links.
Now consider the following:
String[] array = {"hi", "there", "everyone"}
I would suggest that in this situation, strings exist somewhere with all the other constants in memory, and then the array contains references to these constants in memory? So, again, in raw memory, the array is not like ['h', 'i', '\0', 't', 'h', 'e', 'r', 'e'... (etc)] . (using c-style for convenience). Rather, it is more like ['a83a3edf' ,'a38decd' ... (etc)] , where each element is a memory location (link).
My conclusion from this thought process is that in Java you can never think of arrays as buckets of adjacent objects in memory, but rather as continuous references. I can think of no way to guarantee that objects will always be stored in Java.
Now consider C:
char *a = "hi"; char *b = "there"; char *c = "everyone"; char *array[] = {a, b, c};
The above code is functionally equivalent to Java above - that is, the array contains references (pointers) to another memory location. Like Java, the objects pointed to are not necessarily contiguous.
HOWEVER, in the following C code:
struct my_struct array[5]; // allocates 5 * size(my_struct) in memory! NOT room for 5 // references/pointers, but room for 5 my_structs.
The structures in array ARE are adjacent to raw memory.
Now for my specific questions:
Was I right in assuming that in Java arrays should ALWAYS contain links, since the programmer only ever has access to links in Java? What about raw data types? Will it work differently then? Will an int array in Java look the same as one in C in raw memory (except that it adds an Object class, a cool Java class)?
In Java, is there no way for a programmer to guarantee continuous allocation of memory by objects? This can happen by chance or with high probability, but the programmer can not GUARANTEE that this will be so?
In C, programmers MAY create arrays of arrays of objects (structures) contiguously in memory, as I showed above, right?
How do other languages ββdeal with this? I assume Python works like Java?
The motivation for this question is that I want to get a clear idea of ββwhat is happening at the raw memory level with arrays in these languages. Mostly for questions related to the programmer. In a previous interview, I said that an array (not in any language, as a rule, as a whole) holds objects contiguously in memory, for example, buckets. Only after I said this did I realize that itβs not quite the way it works in a language such as Java. Therefore, I want to be 100% on it.
Thanks. Let me know if something needs clarification.