Java 虚拟机的字节序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/981549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:50:44  来源:igfitidea点击:

Java's Virtual Machine's Endianness

javavirtual-machineendianness

提问by JoeCool

What endianness does Java use in its virtual machine? I remember reading somewhere that it depends on the physical machine it's running on, and then other places I have read that it is always, I believe, big endian. Which is correct?

Java 在其虚拟机中使用什么字节序?我记得在某处读到它取决于它运行的物理机器,然后在其他地方我读到它总是,我相信,大端。哪个是正确的?

采纳答案by coobird

Multibyte data in the classfiles are stored big-endian.

class文件中的多字节数据以big-endian 存储。

From The Java Virtual Machine Specification, Java SE 7 Edition, Chapter 4: The classFile Format:

来自Java 虚拟机规范,Java SE 7 版第 4 章:class文件格式

A class file consists of a stream of 8-bit bytes. All 16-bit, 32-bit, and 64-bit quantities are constructed by reading in two, four, and eight consecutive 8-bit bytes, respectively. Multibyte data items are always stored in big-endian order, where the high bytes come first.

类文件由 8 位字节流组成。所有 16 位、32 位和 64 位数量都是通过分别读取两个、四个和八个连续 8 位字节来构造的。多字节数据项始终以大端顺序存储,其中高字节在前。

Furthermore, the operand in an bytecode instruction is also big-endian if it spans multiple bytes.

此外,如果字节码指令中的操作数跨越多个字节,它也是大端的。

From The Java Virtual Machine Specification, Java SE 7 Edition, Section 2.11: Instruction Set Summary:

来自Java 虚拟机规范,Java SE 7 版第 2.11 节:指令集摘要

If an operand is more than one byte in size, then it is stored in big-endian order-high-order byte first. For example, an unsigned 16-bit index into the local variables is stored as two unsigned bytes, byte1and byte2, such that its value is (byte1 << 8) | byte2.

如果一个操作数的大小超过一个字节,那么它首先按大端顺序-高位字节存储。例如,局部变量中的无符号 16 位索引存储为两个无符号字节byte1byte2,因此其值为(byte1 << 8) | byte2

So yes, I think it can be said that the Java Virtual Machine uses big-endian.

所以是的,我认为可以说Java虚拟机使用大端。

回答by Tom Hawtin - tackline

The actual working data stored in the running process will almost certainly match the endianess of the executing process. Generally file formats (including class files) will be in network order (big endian).

存储在运行进程中的实际工作数据几乎肯定会与执行进程的字节序匹配。通常文件格式(包括类文件)将采用网络顺序(大端)。

It's generally difficult to tell what the machine is doing underneath, as it is abstracted away by the virtual machine. You can't cast a short[]to byte[]as you can in C and C++. java.nio.ByteOrder.nativeOrder()should give you the underlying endianess. Matching endianess is useful when using non-byte NIO buffers.

通常很难知道机器在下面做什么,因为它被虚拟机抽象掉了。您不能像在 C 和 C++ 中那样将 a 强制short[]转换byte[]为。java.nio.ByteOrder.nativeOrder()应该给你底层的字节序。使用非字节 NIO 缓冲区时,匹配字节序很有用。