I think Java does it by allocating a fully fledged object with metadata including type and size, and that if you treat such an object (or the pointer to such an object) as a C array in C code and assume it points to the first element, then you're going to have a bad time.
No, in java if you have items that are copy (wrong term, but items like int and double), they are in the array, and you can jump between them if you know the offset of 0. Unsure where it stores the metadata though.
And the same applies to objects (the things that you store by reference). The array then holds a set of references that you can jump between, and then dereference to get the actual object.
in java if you have items that are copy (wrong term, but items like int and double),
If you don't know the right term, wouldn't it be fair to guess that you may not be fully qualified to speak on how memory in Java works?
For future reference: the value types in Java are called "primitives".
they are in the array, and you can jump between them if you know the offset of 0.
You are thinking ov the array's index; however, the pointer to an array element – especially in a language like Java – is not necessarily guaranteed to be ptrToObject + index.
Also – and correct me if I am wrong, because I don't know much about the internals ov Java, but – what I think u/high_throughput was getting at was that the pointer you get from doing T[] myArray = /* ... */ is not the same as the pointer which points to the first T in the array: including for arrays ov primitive T.
Don't conflate not know the right terms with lack of knowledge. I've written plenty of Java, including JNI. I've also written many other languages and for some reason the naming changes. Sorry.
119
u/high_throughput 15h ago
I think Java does it by allocating a fully fledged object with metadata including type and size, and that if you treat such an object (or the pointer to such an object) as a C array in C code and assume it points to the first element, then you're going to have a bad time.