java字节码 - 小于int的类型的表示 [英] java bytecode - representation of types smaller than int

查看:264
本文介绍了java字节码 - 小于int的类型的表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In one of the projects at my university I am working directly with Java bytecode.

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

After browsing the list of instructions available for the JVM (http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings) I saw that there is no such thing as [b|c|s]store, only istore for storing integers in a local variable. Does it mean that if in my program I write:

short a;
int b;

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

I am not saving any memory, because every local variable entry occupies 4 bytes?

我总是认为使用字节类型会节省运行时的一些内存。

I was always under the impression that using short or byte types will save some memory at runtime.

推荐答案

这在 JVMS的2.11.1


请注意,大多数指令 [...] 没有整数类型 byte 的表单, char short 。没有布尔类型的表单。编译器使用Java签名扩展这些值的Java虚拟机指令编码类型 byte short 的文字值加载编译时或运行时类型为 int 的值。 [...] 因此,对实际类型的值的大多数操作 boolean byte char short 由运行于计算类型 int <的值的指令正确执行/ code>。

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.

因此是合理的:


鉴于Java虚拟机的单字节操作码大小,编码类型到操作码会对其指令集的设计造成压力。如果每个类型化指令都支持所有Java虚拟机的运行时数据类型,则会有比指令更多的指令。相反,Java虚拟机的指令集为某些操作提供了降低级别的类型支持。换句话说,指令集故意不正交。可以根据需要使用单独的指令在不支持和支持的数据类型之间进行转换。

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.

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

However, whilst this applies to load/store of stack variables, it doesn't apply to load/store into primitive arrays; there are opcodes for all primitive types.

这篇关于java字节码 - 小于int的类型的表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆