gpt4 book ai didi

java - 移位运算符 - 操作数必须可转换为整数基元?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:12:56 25 4
gpt4 key购买 nike

我正在准备参加 Java 考试,并且正在阅读“OCA Java SE 8 程序员学习指南(考试 1Z0-808)”。在运营商部分,我发现了这句话:

Shift Operators: A shift operator takes two operands whose type must be convertible to an integer primitive.

我觉得很奇怪,所以我用 long 测试了它:

public class HelloWorld{

public static void main(String []args){
long test = 3147483647L;
System.out.println(test << 1);

}
}

它成功了,没有编译器错误,结果是正确的。这本书有错误还是我误解了书中的引述?

最佳答案

移位运算符 >><<JLS section 15.19 中定义.引用:

Unary numeric promotion (§5.6.1) is performed on each operand separately. (Binary numeric promotion (§5.6.2) is not performed on the operands.)

It is a compile-time error if the type of each of the operands of a shift operator, after unary numeric promotion, is not a primitive integral type.

当谈到“原始整数”时,这本书实际上是在谈论“原始整数类型”(在 JLS section 4.2.1 中定义):

The values of the integral types are integers in the following ranges:

  • For byte, from -128 to 127, inclusive
  • For short, from -32768 to 32767, inclusive
  • For int, from -2147483648 to 2147483647, inclusive
  • For long, from -9223372036854775808 to 9223372036854775807, inclusive
  • For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535

关于java - 移位运算符 - 操作数必须可转换为整数基元?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32821196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com