java - 为什么在 Java 中必须将字节上的按位运算强制转换为字节?

标签 java

为什么这里需要 Actor 阵容?

byte a = 0b0000_0000;
byte b = (byte) ~a;

a 是一个字节,b 是一个字节...为什么需要转换?

最佳答案

因为Java Language Specification这么说

Unary numeric promotion (§5.6.1) is performed on the operand. The type of the unary bitwise complement expression is the promoted type of the operand.

and

Otherwise, if the operand is of compile-time type byte, short, or char, it is promoted to a value of type int by a widening primitive conversion (§5.1.2).

int 类型的值不能分配给 byte 类型的变量。

关于java - 为什么在 Java 中必须将字节上的按位运算强制转换为字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29360935/

相关文章:

java - 在 IntelliJ 中调试 Web 容器

java - 统计一个字符在字符串中连续出现的次数

java - sendMessage() 方法在哪里找?

Java - 如何将此字符串转换为日期?

java - 无法使用 Java 和 Apache POI 写入 Excel 文件

java - 过去 24 小时的总计时间

java - 无法准备声明;嵌套异常是 org.hibernate.exception.JDBCConnectionException : could not prepare statement

Java泛型将具体类列表的 future 转换为接口(interface)列表的 future ?

java - 如何使用 java.sql 包中的 rs.getSQLXML() 函数从 Oracle 数据库获取 XMLType 列?

带有自定义控件的 JavaFX8 TableView