第5.1.2节和第5.6.2条没有提到数值提升和扩展常量是如何工作的。
如下所示,出现了预期的错误:
short a = 2;
short b = 3;
short s = a + b; // error: incompatible types: possible lossy conversion from int to short但是,如果它们被声明为“最终”,则不会出错地进行编译:
final short a = 2;
final short b = 3;
short s = a + b; // no error为什么会这样呢?规格的哪个部分解释了这一点?
我的猜测是,它们是编译时间常数,因此被视为整数。
https://stackoverflow.com/questions/50132448
复制相似问题