修复“不兼容类型:可能从int到字节”的损失转换在爪哇

发布于 2025-01-30 12:24:59 字数 745 浏览 2 评论 0原文

我在这里对我的代码有疑问:

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:

error pointing to the ampersand in "byte b1 = ((byte) x) & ((byte) 0xff);"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尝蛊 2025-02-06 12:24:59

你似乎很困惑。

您的代码没有意义。采取任何数字,计算该& 0xff,然后将其存储在字节中,始终是noop - 它无能为力。

您还会出现错误,因为&本质上总是产生至少int(它将匹配较小的内容),因此您正在尝试将INT分配给字节。

您想完成什么?

“我想让我的字节不签名”!

没有可以。 Java没有未签名的字节。签署了Java字节。时期。它可以保持-128和+127之间的值。出于计算目的,-128和255是相同的(它们都是位序列1111 1111 - 在十六进制中,0xff,它们的作用相同在所有相关算术中,尽管将它们转换为另一个数字类型int时确实会很棘手。

“我只想存储255”!

然后使用int。这是大多数& 0xff您在Java代码中看到的是:当您具有Java固有地对待签名的字节值时,您希望将其视为无符号,因此(鉴于在Java中,那就是),您想将其抬高到一个INT,其中包含 unsigned 表示。这就是这样做的方法:

int x = y & 0xFF;

其中y是任何字节。

您大概是在某个地方看到的,现在正在尝试应用它,但是分配了y&的结果。 0xff to 字节并不意味着什么。您将其分配给int变量,或者只是在进一步的计算中使用它作为表达式(y& 0xffint - 确保添加适当的括号,&可能具有意外优先级)。

  int x = 36;
 
    byte b1 = ((byte) x) & ((byte) 0xff); 
   

实际工作的每种可想象的方式都意味着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 an int (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 type int).

"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:

int x = y & 0xFF;

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 a byte doesn't mean anything. You'd assign it to an int variable, or just use it as expression in a further calculation (y & 0xFF is an int - make sure you add the appropriate parentheses, & has perhaps unexpected precedence).

int x = 36;
    byte b1 = ((byte) x) & ((byte) 0xff); 

Every imaginable way of this actually working would mean that b1 is... still 36.

沫尐诺 2025-02-06 12:24:59

计算x& y在两个操作数是字节的情况下,必须首先将它们晋升为int值。字节之间没有&/code>。因此,结果是类型int

即,您所写的内容是有效地评估的,就像您将其编写为以下内容一样,明确地隐含了语言给您的内容:

byte b1 = ((int) (byte) x) & ((int) (byte) 0xff);

只需执行算术,然后施放结果为字节

byte b1 = (byte)(x & 0xff);

/a>

编辑要添加,这要归功于@rzwitserloot,用0xff掩盖字节值是毫无意义的。如果您需要从整数到字节的作业,只需写下演员:

byte b1 = (byte)x;

To compute x & y where the two operands are bytes, they must first be promoted to int values. There is no & between bytes. The result is therefore of type int

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:

byte b1 = ((int) (byte) x) & ((int) (byte) 0xff);

Just do the arithmetic and then cast the result to byte.

byte b1 = (byte)(x & 0xff);

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:

byte b1 = (byte)x;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文