简体   繁体   English

初始化java字节数组:可能会丢失精度

[英]Initialize java byte array: possible loss of precision

I am initializing a byte[] in Java , using hexadecimal or binary notation. 我正在使用十六进制二进制表示法在Java中初始化一个byte[]

It seems that auto-casting to byte is not done when the value approaches the capacity of a byte. 当值接近一个字节的容量时,似乎不会自动转换为字节。

Solution is explicit cast to byte . 解决方案是显式转换为byte

$ javac bitsandbytes/ByteBufferTest.java 
bitsandbytes/ByteBufferTest.java:9: error: possible loss of precision
    static byte[] byteArray = { 0xFF, (byte) 0xCC, 0xCC, 0b1000_0000, 0x2F, 0x01 };
                                ^
  required: byte
  found:    int
bitsandbytes/ByteBufferTest.java:9: error: possible loss of precision
    static byte[] byteArray = { 0xFF, (byte) 0xCC, 0xCC, 0b1000_0000, 0x2F, 0x01 };
                                                   ^
  required: byte
  found:    int
bitsandbytes/ByteBufferTest.java:9: error: possible loss of precision
    static byte[] byteArray = { 0xFF, (byte) 0xCC, 0xCC, 0b1000_0000, 0x2F, 0x01 };
                                                         ^
  required: byte
  found:    int
3 errors

在此输入图像描述

It seems that auto-casting to byte is not done when the value approaches the capacity of a byte. 当值接近一个字节的容量时,似乎不会自动转换为字节。

No, it happens when the value is out of range. 不,它在值超出范围时发生。 A byte in Java is in the range [-128, 127]. Java中的一个byte在[-128,127]范围内。 So 0xcc is out of range, for example... although this has nothing to do with which base you express the literal in. 所以0xcc超出了范围,例如......虽然这与你表达文字的基数无关。

If you have: 如果你有:

byte[] x = { 127 };
System.out.println(x[0]);

that will print 127, because it's in the range of byte . 这将打印127,因为它在byte范围内。 If you have: 如果你有:

byte[] x = { (byte) 128 };
System.out.println(x[0]);

... that will print -128. ...将打印-128。 That would be very unexpected if you didn't have an explicit cast. 如果您没有明确的演员表,那将是非常意外的。

Admittedly an error message about "precision" is odd - and not what I receive. 不可否认,有关“精确度”的错误消息很奇怪 - 而不是我收到的消息。 I see this: 我看到了这个:

error: incompatible types: possible lossy conversion from int to byte 错误:不兼容的类型:可能从int到byte的有损转换

It is a lossy conversion, so that's fine. 一个有损转换,所以没关系。 I wouldn't talk about that being a precision issue though, which is what I'd expect for a conversion of (say) double to float . 我不会谈论这是一个精确的问题,这是我期望将(例如) double转换为float

In terms of the JLS, the relevant section is 5.2 , which applies to each VariableInitializer in the ArrayInitializer. 就JLS而言,相关部分是5.2 ,它适用于ArrayInitializer中的每个VariableInitializer。

This is the important part (emphasis mine): 这是重要的部分(强调我的):

In addition, if the expression is a constant expression (§15.28) of type byte , short , char , or int : 此外,如果表达式是byteshortcharint类型的常量表达式(第15.28节):

  • A narrowing primitive conversion may be used if the type of the variable is byte , short , or char , and the value of the constant expression is representable in the type of the variable . 如果变量的类型是byteshortchar ,则可以使用缩小的基元转换, 并且常量表达式的值可以在变量的类型中表示

The examples you've given aren't representable in the byte type, hence the error. 您给出的示例在byte类型中无法表示,因此出现错误。

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

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