据我了解,变量评估是在运行时完成的。但是,类型评估是在Java中的编译时完成的。
同样,正如我所见,使变量恒定(我正在使用局部变量,但是上面的概念没有改变),将在编译时知道其值。
我提供了两个示例来测试这个概念。第一个起作用,第二个不起作用。
有人可以向我解释为什么将变量设为常量可以使我将短变量分配给int变量,而不能将int变量分配给long吗?
// Working example
final int x = 10;
short y = x;
// Non-working example
final long a = 10L;
int b = a;
Run Code Online (Sandbox Code Playgroud)