简体   繁体   English

Java语言规范在哪里定义了运行时字节序?

[英]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. 看来Java运行时是Big Endian,但是我找不到对它的引用,仅用于JVM的类文件规范。

I'm looking for a definitive place in the JLS (regardless of version) which specifies that: 我正在JLS(无论版本)中寻找明确的位置,该位置指定:

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

and not lastByte == 16 ( 0x10 ) 而不是lastByte == 160x10

OR where it specifies that this is platform/JVM dependent. 或在其中指定依赖于平台/ JVM的位置。

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. 这与语言无关,而与虚拟机有关,这就是为什么要在Java虚拟机规范中定义它,而不是在Java语言规范中定义它。

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: 或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. 现在,人们可以争论一个事实,即0x0000000F代表15 ,这意味着大字节序。 This is at least implicitly defined in the definition of the lexical structure, in JLS Section 3.10.1, Integer Literals : 这至少是在JLS第3.10.1节“整数文字 ”的词法结构的定义中隐式定义的:

The largest positive hexadecimal, octal, and binary literals of type int - each of which represents the decimal value 2147483647 (2^31-1) - are respectively: int类型的最大正十六进制,八进制和二进制文字(分别代表十进制值2147483647(2 ^ 31-1))分别为:

  • 0x7fff_ffff, 0x7fff_ffff,
  • 0177_7777_7777, and 0177_7777_7777和
  • 0b0111_1111_1111_1111_1111_1111_1111_1111 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: 除此之外,字节序主要与存储和通信有关,但是这些不是语言方面,可以通过诸如ByteOrder类或在API级别上(例如在DataOutputStream :: writeInt方法中)来实现:

Writes an int to the underlying output stream as four bytes, high byte first. 将int以四个字节的形式写入基础输出流,高字节在前。


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: 关于移位运算符JLS第15.19节指出:

The value of n << s is n left-shifted s bit positions; n << s的值是n个左移的s位位置; this is equivalent (even if overflow occurs) to multiplication by two to the power s. 这等效于(即使发生溢出)乘以2的幂s。

The value of n >> s is n right-shifted s bit positions with sign-extension. n >> s的值是带有符号扩展名的n个右移s位位置。 The resulting value is [ n / 2s ]. 结果值为[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. 对于n的非负值,这等效于将整数除法运算符/计算出的整数除法以2乘以幂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...) 这里的规范规定,在现有个位移“左”,并在同一时间,即“左”是“越显著位置”(然而,人们也可以说, <<手段在Little-“右移” Endian世界...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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