简体   繁体   English

Java-用字节索引到数组

[英]Java - indexing into array with a byte

Is it possible to index a Java array based on a byte? 是否可以基于字节索引Java数组?

ie something like 即像

array[byte b] = x;

I have a very performance-critical application which reads b (in the code above) from a file, and I don't want the overhead of converting this to an int. 我有一个非常关键的应用程序,它从文件中读取b(在上面的代码中),我不希望将其转换为int的开销。 What is the best way to achieve this? 实现此目标的最佳方法是什么? Is there a performance-decrease as a result of using this method of indexing rather than an int? 使用这种索引方法而不是int会导致性能下降吗?

With many thanks, 非常感谢

Froskoy. 弗罗斯科伊。

There's no overhead for "converting this to an int." “将其转换为int”没有任何开销。 At the Java bytecode level, all byte s are already int s. 在Java字节码级别,所有byte s已经是int

In any event, doing array indexing will automatically upcast to an int anyway. 无论如何,执行数组索引将自动将其转换为int None of these things will improve performance, and many will decrease performance. 所有这些都不会提高性能,而很多都会降低性能。 Just leave your code using an int . 只需将代码保留为int

The JVM specification, section 2.11.1 : JVM规范,第2.11.1节

Note that most instructions in Table 2.2 do not have forms for the integral types byte, char, and short. 请注意,表2.2中的大多数指令都没有整数类型byte,char和short的形式。 None have forms for the boolean type. 没有一个具有布尔类型的形式。 Compilers encode 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 runtime. 编译器使用Java虚拟机指令对字节和short类型的文字值的负载进行编码,这些指令在编译时或运行时将这些值符号扩展为int类型的值。 Loads of literal values of types boolean and char are encoded using instructions that zero-extend the literal to a value of type int at compile-time or runtime. 使用布尔值和char类型的文字值的负载使用指令进行编码,这些指令在编译时或运行时将文字零扩展为int类型的值。 Likewise, loads from arrays of values of type boolean, byte, short, and char are encoded using Java virtual machine instructions that sign-extend or zero-extend the values to values of type int. 同样,使用Java虚拟机指令对布尔,字节,短和字符类型的值数组中的负载进行编码,这些指令将这些值进行符号扩展或零扩展为int类型的值。 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. 因此,对实际类型为boolean,byte,char和short的值的大多数操作均由对计算类型为int的值进行操作的指令正确执行。

As all integer types in java are signed you have anyway to mask out 8 bits of b's value provided you do expect to read from the file values greater than 0x7F: 由于Java中所有的整数类型都是带符号的,因此只要您希望从文件中读取大于0x7F的值,就可以屏蔽掉b值的8位:

byte b;
byte a[256];
a [b & 0xFF] = x;

No; 没有; array indices are non-negative integers (JLS 10.4) , but byte indices will be promoted. 数组索引是非负整数(JLS 10.4) ,但是字节索引将得到提升。

No, there is no performance decrease, because on the moment you read the byte, you store it in a CPU register sometime. 不,不会降低性能,因为在读取字节时,有时会将其存储在CPU寄存器中 Those registers always works with WORDs , which means that the byte is always "converted" to an int (or a long, if you are on a 64 bit machine). 这些寄存器始终与WORD一起使用 ,这意味着该字节始终被“转换”为int(如果您使用的是64位计算机,则为long)。

So, simply read your byte like this: 因此,只需像这样读取您的字节:

int b = (in.readByte() & 0xFF);

If your application is that performance critical, you should be optimizing elsewhere. 如果您的应用程序对性能至关重要,则应在其他地方进行优化。

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

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