gpt4 book ai didi

java 字节码 - 小于 int 的类型的表示

转载 作者:太空狗 更新时间:2023-10-29 22:34:49 24 4
gpt4 key购买 nike

在我大学的一个项目中,我直接使用 Java 字节码。

在浏览了 JVM 可用的指令列表 ( http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings ) 后,我看到没有 [b|c|s]store 之类的东西, 只有 istore 用于将整数存储在局部变量中。这是否意味着如果在我的程序中我写:

short a;
int b;

我没有节省任何内存,因为每个局部变量条目都占用 4 个字节?

我一直认为使用 shortbyte 类型会在运行时节省一些内存。

最佳答案

这在 2.11.1 of the JVMS 节中有解释:

Note that most instructions [...] do not have forms for the integral types byte, char, and short. None have forms for the boolean type. A compiler encodes loads of literal values of types byte and short using Java Virtual Machine instructions that sign-extend those values to values of type int at compile-time or run-time. [...] Thus, most operations on values of actual types boolean, byte, char, and short are correctly performed by instructions operating on values of computational type int.

这是有道理的:

Given the Java Virtual Machine's one-byte opcode size, encoding types into opcodes places pressure on the design of its instruction set. If each typed instruction supported all of the Java Virtual Machine's run-time data types, there would be more instructions than could be represented in a byte. Instead, the instruction set of the Java Virtual Machine provides a reduced level of type support for certain operations. In other words, the instruction set is intentionally not orthogonal. Separate instructions can be used to convert between unsupported and supported data types as necessary.

但是,虽然这适用于堆栈变量的加载/存储,但不适用于原始数组的加载/存储;所有原始类型都有操作码。

关于java 字节码 - 小于 int 的类型的表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17094704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com