java - 为什么 final Byte 作为 switch 语句中的 case 不能编译?

标签 java switch-statement constants

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/

相关文章:

Java - 使用正则表达式捕获可选字段?

java - GLES30.glGetBufferSubData 在哪里?如何读取转换反馈数据opengles 3.0

javascript - 在 javascript 中用 switch/case 替换一堆显示/隐藏

转到教程选择语句

c# - 将 switch 语句转换为更优雅的解决方案

const 参数在数组定义后发生变化

c++ - 如何在派生类的函数中使用基类的静态常量字段作为数组的大小?

java - 如何使用规范中的 CriteriaQuery 来订购列表?

java - 将新软件安装到 Eclipse

C99 const 按值传递