Search code examples
javaperformancecpu-architectureendiannessopenj9

How does Java Handle Endianess when running on Little Endian CPU Architectures?


Java being Big Endian how does it handle Little Endian CPUs while maintaining performance? Does JVM (OpenJDK, OpenJ9, etc.) do any special optimisations to maintain performance like only selectively being Big Endian in special situation in Little Endian platforms? Is there special endianess handling when accessing ByteBuffers or calling native code or writing to IO or accessing volatile variables? How does Java change the endianess in Little Endian architectures? At what point or operation (load, store, calculation, registers, cache, memory, etc.) is the endianess changed? What kind of performance penalty would this have?


Solution

  • Java being Big Endian how does it handle Little Endian CPUs while maintaining performance?

    Java is not Big Endian. In the few places in the Java Runtime Library where Endianness is even an issue, the API uses Big Endian, but it is always well-documented, and some of the APIs allow you to specify the Endianness you want.

    Does JVM (OpenJDK, OpenJ9, etc.) do any special optimisations to maintain performance like only selectively being Big Endian in special situation in Little Endian platforms?

    No, the JVM uses the native Endianness.

    Is there special endianess handling when accessing ByteBuffers or calling native code or writing to IO or accessing volatile variables?

    Yes, No, Yes, and No.

    Since the JVM uses native byte order, there is no handling needed for calling native code or accessing volatile variables. Byte order only matters when (de)serializing to/from bytes, e.g. when accessing ByteBuffers or writing to IO.

    How does Java change the endianess in Little Endian architectures?

    Same way you would change Endianness anywhere, it swaps the bytes, or read/writes the bytes in the appropriate order.

    At what point or operation (load, store, calculation, registers, cache, memory, etc.) is the endianess changed?

    It's not, since the JVM uses the native Endianness. Endianness is only applied when the native value is converted to/from bytes. At no other point in time does Endianness matter.

    What kind of performance penalty would this have?

    None, since it doesn't do anything.