简体   繁体   中英

Where runtime Endianness is defined in the Java Language Specification?

It seems that Java runtime is Big Endian, but I can't find a reference to this, only for the class file specification of JVMs.

I'm looking for a definitive place in the JLS (regardless of version) which specifies that:

int value = 4096; // 0b0001 0000 0000 0000 = 0x10 00
                  //               \   /           |                             
int lastByte = value & 0xFF; //      |             |
assert lastByte == 0; // ---------------------------

and not lastByte == 16 ( 0x10 )

OR where it specifies that this is platform/JVM dependent.

This is not so much a matter of the language , but rather of the virtual machine - that's why it is defined in the Java Virtual Machine Specifiction, but not in the Java Language Specification.

In fact, the results of these bitwise computations are independent of the endianness. Assume Big-Endian:

int value = 4111;                //   0x0000100F
int lastByte = value & 0xFF;     // & 0x000000FF
                                 // = 0x0000000F

Or Little-Endian:

int value = 4111;                //   0xF0010000
int lastByte = value & 0xFF;     // & 0xFF000000
                                 // = 0xF0000000

In both cases, the result is the same (in either of both forms).


One could now argue about the fact that 0x0000000F stands for 15 , which implies big-endianness. This is at least implicitly defined in the definition of the lexical structure, in JLS Section 3.10.1, Integer Literals :

The largest positive hexadecimal, octal, and binary literals of type int - each of which represents the decimal value 2147483647 (2^31-1) - are respectively:

  • 0x7fff_ffff,
  • 0177_7777_7777, and
  • 0b0111_1111_1111_1111_1111_1111_1111_1111

Apart from that, the endianness is mainly relevant for storage and communication, but these are not language aspects and facilitated with things like the ByteOrder class, or on an API-level, like in the DataOutputStream::writeInt method:

Writes an int to the underlying output stream as four bytes, high byte first.


The only part where the endianness could be considered to influence the semantics of the language are the shift operations. But even there, it's mainly a matter of the interpretation of the language. The JLS Section 15.19 about Shift Operators states:

The value of n << s is n left-shifted s bit positions; this is equivalent (even if overflow occurs) to multiplication by two to the power s.

The value of n >> s is n right-shifted s bit positions with sign-extension. The resulting value is [ n / 2s ]. For non-negative values of n, this is equivalent to truncating integer division, as computed by the integer division operator /, by two to the power s.

The specification here states that the existing bits are shifted "left", and at the same time, that "left" is "the more significant position" (However, one could also say that << means "shifting right" in a Little-Endian world...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM