byte a = 125;
final byte b = 2;
final Byte c = 3;
switch (a) {
case b: // works fine
break;
case c: // Constant Expression required
break;
}
因为 c
是一个 final
变量,它不是一个编译时间常量,因此不是一个有效的 case 标签吗?
最佳答案
Since c is a final variable, isn't it a compile time constant
没有。 JLS 15.28 中给出了常量表达式的规则,并且它们不包括包装器类型:
A constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following [...]
包装器类型既不是原始类型也不是 String
。
关于java - 为什么 final Byte 作为 switch 语句中的 case 不能编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30782446/