Add java21-features module: runnable demos and captured output for the ankurm.com java21-features article

This commit is contained in:
2026-10-20 17:13:59 +00:00
parent 5a076c826f
commit c3cc57f482
33 changed files with 354 additions and 0 deletions
@@ -0,0 +1,5 @@
/** Fails to compile on purpose: a more general pattern before a specific one makes the later case unreachable. */
public class DominatedCase {
static String f(Object o) { return switch (o) { case CharSequence cs -> "cs"; case String s -> "s"; default -> "d"; }; }
public static void main(String[] args) { }
}
@@ -0,0 +1,8 @@
/** Fails to compile on purpose: a pattern switch over a sealed type must cover every permitted subtype. */
public class NonExhaustive {
sealed interface S permits A, B {}
record A() implements S {}
record B() implements S {}
static String f(S s) { return switch (s) { case A a -> "a"; }; }
public static void main(String[] args) { }
}
@@ -0,0 +1,4 @@
/** Fails to compile on purpose: string templates are a preview feature in 21 and are off by default. */
public class PreviewWithoutFlag {
public static void main(String[] args) { String n = "x"; System.out.println(STR."hi \{n}"); }
}