且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

编译时常量和变量

更新时间:2022-04-04 22:39:21

编译时间常数必须为:


  • 宣布最终

  • 原语或字符串

  • 在声明中初始化

  • 使用常量表达式初始化

  • declared final
  • primitive or String
  • initialized within declaration
  • initialized with constant expression

所以私人决赛int x = getX(); 不是常数。

到secon d问题 private int y = 10; 不是常量(在这种情况下是非final),因此优化器无法确定该值将来是否会发生变化。所以它不能像恒定值那样优化它。答案是:不,它的处理方式与编译时间常数不同。

To the second question private int y = 10; is not constant (non-final in this case), so optimizer cannot be sure that the value would not change in the future. So it cannot optimize it as good as constant value. The answer is: No, it is not treated the same way as compile time constant.