Can switch expression be compile-time constant?

Tagir Valeev amaembo at gmail.com
Thu Nov 22 10:18:19 UTC 2018


Hello!

It seems that switch expressions are not compiled as compile-time
constant (using Java12 ea builds). E.g.:

public class Constant {
  public static void main(String[] args) {
    int x = switch(0) {case 1 -> 2; case 2 -> 3; default -> 1;};
    int y = 0 == 1 ? 2 : 0 == 2 ? 3 : 1;
    System.out.println(x);
  }
}

The equivalent conditional expression is folded into constant 1, but
switch is present in bytecode as is. Is it deliberate decision to
exclude switches from compile-time constant?

With best regards,
Tagir Valeev


More information about the amber-spec-experts mailing list