import java.util.List; /** * Inspired by examples in https://openjdk.java.net/jeps/420 */ public class SwitchPatternPreview2OK { public static void main(String[] args) { constantLabelMustAppearBeforePattern(-1); constantLabelMustAppearBeforePattern(0); constantLabelMustAppearBeforePattern(42); constantLabelMustAppearBeforePattern(-99); constantLabelMustAppearBeforePattern(Integer.valueOf(123)); constantLabelMustAppearBeforePattern(null); System.out.println(testGenericSealedExhaustive(new B())); } static String constantLabelMustAppearBeforePattern(Object o) { switch (o) { case -1, 1 -> System.out.println("special case:" + o); case Integer i && i > 0 -> System.out.println("positive integer: " + o); case Integer i -> System.out.println("other integer: " + o); default -> System.out.println("non-integer: " + o); } return o.toString(); } sealed interface I permits A, B {} final static class A implements I {} final static class B implements I {} static int testGenericSealedExhaustive(I i) { return switch (i) { // Exhaustive as no A case possible! case B bi -> 42; }; } }