修复“不兼容类型:可能从int到字节”的损失转换在爪哇
我在这里对我的代码有疑问:
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World\n");
int x = 36;
byte b1 = ((byte) x) & ((byte) 0xff); // it seems it is the part after &, but I have 0xff cast to byte by using (byte)0xff, so not sure where exactly the error is coming from.
System.out.println(b1);
}
}
我不确定确切的哪一部分导致错误的错误:
不兼容的类型:可能从int到字节的损失转换
这是从程序输出的错误消息:
“ https://i.sstatic.net/ghbia.png” alt =“指向ampersand in&quot”字节b1 =(((byte)x)x)&((byte)0xff); a>
I have question regarding my code here:
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World\n");
int x = 36;
byte b1 = ((byte) x) & ((byte) 0xff); // it seems it is the part after &, but I have 0xff cast to byte by using (byte)0xff, so not sure where exactly the error is coming from.
System.out.println(b1);
}
}
I am not sure exactly which part is causing the error of:
incompatible types: possible lossy conversion from int to byte
This is the error message output from the program:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你似乎很困惑。
您的代码没有意义。采取任何数字,计算该
& 0xff
,然后将其存储在字节中,始终是noop - 它无能为力。您还会出现错误,因为
&
本质上总是产生至少int
(它将匹配较小的内容),因此您正在尝试将INT分配给字节。您想完成什么?
“我想让我的字节不签名”!
没有可以。 Java没有未签名的字节。签署了Java字节。时期。它可以保持-128和+127之间的值。出于计算目的,-128和255是相同的(它们都是位序列
1111 1111
- 在十六进制中,0xff
,它们的作用相同在所有相关算术中,尽管将它们转换为另一个数字类型int
时确实会很棘手。“我只想存储255”!
然后使用
int
。这是大多数& 0xff
您在Java代码中看到的是:当您具有Java固有地对待签名的字节值时,您希望将其视为无符号,因此(鉴于在Java中,那就是),您想将其抬高到一个INT,其中包含 unsigned 表示。这就是这样做的方法:其中
y
是任何字节。您大概是在某个地方看到的,现在正在尝试应用它,但是分配了
y&的结果。 0xff
to字节
并不意味着什么。您将其分配给int
变量,或者只是在进一步的计算中使用它作为表达式(y& 0xff
是int
- 确保添加适当的括号,&
可能具有意外优先级)。实际工作的每种可想象的方式都意味着
b1
是...仍然36。You appear to be confused.
There is no point in your code. taking any number, calculating that
& 0xFF
, and then storing it in a byte, is always a noop - it does nothing.You additionally get an error because
&
inherently always produces at least anint
(it'll upcast anything smaller to match), so you're trying to assign an int to a byte.What are you trying to accomplish?
"I want to have my byte be unsigned"!
No can do. Java doesn't have unsigned bytes. A java byte is signed. Period. It can hold a value between -128 and +127. For calculation purposes, -128 and 255 are identical (they are both the bit sequence
1111 1111
- in hex,0xFF
, and they act identically under all relevant arithmetic, though it does get tricky when converting them to another numeric typeint
)."I just want to store 255"!
Then use
int
. This is where most& 0xFF
you'll ever see in java code comes from: When you have a byte value which java inherently treats as signed, but you wish to treat it as unsigned and, therefore (given that in java bytes can't do that), you want to upcast it to an int, containing the unsigned representation. This is how to do that:Where
y
is any byte.You presumably saw this somewhere and are now trying to apply it, but assigning the result of
y & 0xFF
to abyte
doesn't mean anything. You'd assign it to anint
variable, or just use it as expression in a further calculation (y & 0xFF
is anint
- make sure you add the appropriate parentheses,&
has perhaps unexpected precedence).Every imaginable way of this actually working would mean that
b1
is... still 36.计算
x& y
在两个操作数是字节的情况下,必须首先将它们晋升为int
值。字节之间没有&/code>。因此,结果是类型
int
,即,您所写的内容是有效地评估的,就像您将其编写为以下内容一样,明确地隐含了语言给您的内容:
只需执行算术,然后施放结果为
字节
。/a>
编辑要添加,这要归功于@rzwitserloot,用0xff掩盖字节值是毫无意义的。如果您需要从整数到字节的作业,只需写下演员:
To compute
x & y
where the two operands are bytes, they must first be promoted toint
values. There is no&
between bytes. The result is therefore of typeint
That is, what you wrote is effectively evaluated as if you'd written it as the following, making explicit what the language gives you implicitly:
Just do the arithmetic and then cast the result to
byte
.Link to Java Language Specification
Edited to add, thanks to @rzwitserloot, that masking a byte value with 0xff is however pointless. If you need the assignment from an integer to a byte, just write the cast: