Total pattern and null pattern

Tagir Valeev amaembo at gmail.com
Wed Jul 20 08:54:45 UTC 2022


Hello!

Playing with Java 19, we stumbled upon this sample:

public class Main {
  public static void main(String[] args) {
    Integer x = null;
    switch (x) {
      case Integer i -> System.out.println(i);
      case null -> System.out.println("null is supplied!");
    }
  }
}

Latest javac 19-ea says that this code is not compilable, as Integer i
is dominated by null. However, if we remove `case null`, then `case
Integer i` doesn't match the `null` (NPE is thrown). I find it strange
that `case Integer i` matches null when `case null` is supplied and
I'm forced to use `case null` before `case Integer i`. Is it expected
behavior?

With best regards,
Tagir Valeev.


More information about the amber-spec-experts mailing list