Can switch expression be compile-time constant?

Brian Goetz brian.goetz at oracle.com
Thu Nov 22 13:03:27 UTC 2018


Yes, this was a deliberate choice.  

Sent from my iPad

> On Nov 22, 2018, at 5:18 AM, Tagir Valeev <amaembo at gmail.com> wrote:
> 
> 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