不一致的错误:“所需类型:字节,实际提供类型:整数”(Java中)

huangapple go评论69阅读模式
英文:

Inconsistent "Required type: byte provided: int" in Java

问题

我把整数存储在字节数组中,但突然出现了“所需类型:byte,提供类型:int”的错误,而之前的某些行则没有。所以我试图找出有什么不同,以下是测试:

    byte b;
    int integer = 12;
    final int finalInteger = 12;
    final int finalIntegerLO = 128; // 1000 0000

    b = integer;               //所需类型byte,提供类型int
    b = finalInteger;          //OK
    b = finalIntegerLO;        //所需类型byte,提供类型int

我猜,没有在2^7位置上有'1'的最终整数是可以的吗?这给了我一个想法,如果将其与位运算符结合起来,现在它对我来说就不太有意义了...

    b = finalIntegerLO & 0xf;  //OK

现在是可以的...
但是

    b = integer & 0xf;         //所需类型byte,提供类型int

为什么会有如此不同的行为?有人能解释一下吗?

英文:

I stored integers in byte arrays but suddenly i got a "Required type: byte provided: int" error and some lines above not. So i tried to find out what different was, tests below:

    byte b;
    int integer = 12;
    final int finalInteger = 12;
    final int finalIntegerLO = 128; // 1000 0000

    b = integer;               //Required type byte provided int
    b = finalInteger;          //OK
    b = finalIntegerLO;        //Required type byte provided int

I guess having a final int with no '1' in the 2^7 place is Ok? It gave me an idea what happens if you combine it with bitwise operators and now it makes much less sense to me..

    b = finalIntegerLO & 0xf;  //OK

Is now ok..
but

    b = integer & 0xf;         //Required type byte provided int

not??

Can someone explain me why it acts so different?

答案1

得分: 0

收到执行 b = integer 时收到的错误是 "不兼容的类型:从int到byte的可能有损转换"。当编译器执行此语句时,正在进行从更高数据类型(int)到更低数据类型(byte)的隐式类型转换。这是不可能的,因为这种转换可能会导致信息/精度的丢失,因此Java试图避免这种转换。一种替代方法是使用显式类型转换来强制进行转换,如下所示:

byte b;
int integer = 12;
b = (byte) integer;

继续说,

final int finalInteger = 12;
final int finalIntegerLO = 128;

b = finalInteger 可行,因为 final 关键字确保变量 finalInteger 不会更改其值。由于我们已经告诉Java编译器不能更改 finalInteger 变量的值,并且 finalInteger 存储的值在可以存储在字节变量中的值范围内(-128 到 127),因此从 intbyte 的转换是可能的。
b = finalIntegerLO 不可行,因为 finalIntegerLO 的值超过了字节变量的最大值范围,即 -128 到 127。

英文:

The error that is received while executing b = integer is "incompatible types: possible lossy conversion from int to byte". When this statement is being executed by the compiler, an implicit type conversion from a higher data type(int) to a lower data type(byte) is being carried out. This is not possible as there might be loss of information/precision with such conversions and hence, java tried to avoid them. An alternative would be to force/coerce the conversion using an explicit type conversion as follows:

byte b;
int integer = 12;
b = (byte) integer;

Moving on,

final int finalInteger = 12;
final int finalIntegerLO = 128;

b = finalInteger works because the final keyword ensures that the variable finalInteger does not change its value. Since, we are already telling our Java compiler that we cannot change the value of finalInteger variable and because finalInteger stores a value that is in the range of values that can be stored in a byte variable (-128 to 127), the conversion from int to byte is possible.
b = finalIntegerLO does not work because the value of finalIntegerLO exceeds the maximum value for a byte variable, i.e, -128 to 127.

答案2

得分: 0

让我们逐行分解


案例 1:

b = integer

在这里,我们可以看到我们正在尝试将 int 转换为 byte,因此编译器要求我们显式地进行类型转换,如下所示。(integer 的值可能超出 byte 范围。)

b = (byte) integer;

案例 2:

b = finalInteger;

这个情况略有不同,因为 finalInteger 是一个常量,其值是固定的,编译器可以事先判断它是否在 byte 范围内。如果它在范围内,编译器足够聪明,在不需要显式类型转换的情况下将 int 转换为 byte


案例 3:

b = finalIntegerLO;

Byte 的范围是 -128 到 127,很明显我们不能将 int 转换为 byte,并且编译器看到 finalIntegerLO 是一个常量,因此无法进行这种转换,我们会看到一个错误。

为了消除这个错误,我们可以显式地进行类型转换(尽管不建议这样做),这将使我们得到 b = -128

b = (byte) finalIntegerLO;

案例 4:

b = finalIntegerLO & 0xf;

这里的 finalIntegerLO0xf 都是常量,编译器可以确定结果是 0,这在 byte 范围内。


案例 5:

b = integer & 0xf;

这里 integer 的值可以在执行此行之前更改,因此编译器不确定结果是否在 int 范围内,因此要求我们显式进行类型转换,如下所示。

b = (byte) (integer & 0xf);

案例 3 一样,你可能会得到意外的结果。

英文:

Let's break every line


case 1:

b = integer

Here we can see we are trying to convert int into a byte, so compiler asks as to explicitly typecast like so. (value of integer may exceed byte range.)

b = (byte) integer;

case 2:

b = finalInteger;

This case is slightly different since finalInteger is a constant whose value is fixed and the compiler can tell beforehand whether it lies within the range of byte or not. If it lies within the range compiler is smart enough to convert int to byte without us to explicitly typecast it.


case 3:

b = finalIntegerLO;

Range of byte is -128 to 127 clearly we cannot convert int to a byte and compiler sees that finalIntegerLO is a constant so it is impossible to carry out this conversion and we see an error

To remove this error we can explicitly typecase (DONT DO IT THOUGH) which will give use as b = -128

b = (byte) finalIntegerLO;

case 4:

b = finalIntegerLO & 0xf;

Here finalIntegerLO and 0xf both are constants and compiler can determine what will be the result it's 0 which is within the range of byte.


case 5:

b = integer & 0xf;

Here integer value can be changed before the execution of this line so the compiler is not sure if the result is within the range of int or not, so it asks us to explicitly typecast like so.

b = (byte) (integer & 0xf);

Again like case 3 you may get an unexpected result.

huangapple
  • 本文由 发表于 2020年8月17日 13:35:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63445075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定