No, it is not possible for arrays and buffers to share the same physical memory. Arrays are stored on the heap and buffers off-heap. The reason LWJGL uses buffers is because arrays can be moved at any time by the garbage collector and there is no way to restrict native code from accessing the old location. Well, in theory you can, using JNI's GetPrimitiveArrayCritical/ReleasePrimitiveArrayCritical, but that has serious GC implications and the JVM often has to copy data back and forth. You're correct that array storage is unspecified and may not be contiguous, but in practice they always are, so that's not the primary reason for not using them.
One of the possibilities for
Arrays 2.0 is support for off-heap storage, but I wouldn't hold my breath for it (earliest we could hope for is Java 10).
Buffer access is optimized like array access by the JVM, more or less. In practice, array access may be slightly faster and both are slower than object field access. My recommendation would be:
- For heavy computations, use plain Java objects/arrays, copy to buffer as the final step.
- For simple computations, use buffers directly.
Or use OpenCL or OpenGL compute shaders for computations!
