Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8314226

Series of colon-style fallthrough switch cases with guards compiled incorrectly

XMLWordPrintable

    • 21
    • b15

        Code:

        public class Test {
          void test(Object obj) {
            switch (obj) {
              case Integer _ when ((Integer) obj) > 0:
              case String _ when !((String) obj).isEmpty():
                System.out.println(obj + ": Positive number or non-empty string");
                break;
              default:
                System.out.println("other");
            }
          }

          public static void main(String[] args) {
            new Test().test("");
          }
        }

        This program can be compiled correctly (expected) and prints (unexpectedly)

        ": Positive number or non-empty string"

        It should print "other", as guard condition is not satisfied. Note that if I remove never taken "case Integer _ when ((Integer) obj) > 0:", then the string guard is taken into account.

              abimpoudis Angelos Bimpoudis
              tvaleev Tagir Valeev
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: