简体   繁体   中英

Why final Byte as a case in switch statement doesn't compile?

byte a = 125;
final byte b = 2;
final Byte c = 3;
switch (a) {
case b: // works fine
    break;
case c: // Constant Expression required
    break;
}

Since c is a final variable, isn't it a compile time constant and hence a valid case label?

Since c is a final variable, isn't it a compile time constant

No. The rules for constant expressions are given in JLS 15.28 , and they don't include wrapper types:

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 [...]

A wrapper type is neither a primitive type nor String .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM