I wanted to know about differences in performance between arrays in c++ and java. I know that for object arrays in java, the objects are not stored contiguously in memory, only pointers to these objects are stored contiguously. What about for primitive types like ints? As far as I know the JVM doesn't guarantee that the array will be stored contiguously. I basically want to know what the main differences are in performance of primitive arrays between java and c++. Does bound checking also play a part in slowing arrays in java down?
As far as I know the JVM doesn't guarantee that the array will be stored contiguously.
While I don't see any cast-iron guarantee that the array elements will be stored contiguously, I believe they will for every mainstream implementation. And for primitive types, the primitive values are stored directly - not references. As far as I'm aware, all mainstream implementations pack the arrays too - so if you have a byte[]
for example, it doesn't align each byte on a 4 or 8 byte boundary (thus multiplying the space required by 4 or 8). But a boolean[]
value doesn't pack individual bits (in implementations I've used) - new boolean[n]
takes the same space as new byte[n]
.
See more on this question at Stackoverflow