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 @@
$ javac src/VirtualThreadsDemo.java && java VirtualThreadsDemo (21.0.12.1)
tasks completed : 10000
finished in < 5s: true (10,000 x 1s sleeps)
isVirtual : true, daemon=true
platform isVirtual: false
@@ -0,0 +1,5 @@
$ javac src/RecordPatternsDemo.java && java RecordPatternsDemo (21.0.12.1)
line from (0,0) to (3,4) length^2=25
box of String hi
box of Integer 7
no match
@@ -0,0 +1,3 @@
$ javac src/PatternSwitchDemo.java && java PatternSwitchDemo (21.0.12.1)
big circle | circle | square 2.0 | rect 2.0x3.0
null handled in the switch | large int | int | string of 4 | other
@@ -0,0 +1,11 @@
$ javac src/SequencedCollectionsDemo.java && java SequencedCollectionsDemo (21.0.12.1)
getFirst/getLast: a c
reversed view : [c, b, a]
addFirst+removeLast: [z, a, b]
LinkedHashSet reversed: [3, 2, 1]
firstEntry/lastEntry : x=1 z=3
putFirst : {w=0, x=1, y=2, z=3}
reversed map : {z=3, y=2, x=1, w=0}
view sees add : [3, 2, 1]
immutable list : UnsupportedOperationException
HashSet : no getFirst (unordered)
+5
View File
@@ -0,0 +1,5 @@
$ javac src/KemDemo.java && java KemDemo (21.0.12.1)
algorithm : DHKEM
secrets equal : true
secret length : 32 bytes
ciphertext length: 32 bytes
@@ -0,0 +1,9 @@
$ javac src/GcNamesDemo.java && java -XX:+UseZGC GcNamesDemo (21.0.12.1)
gc bean : ZGC Cycles
gc bean : ZGC Pauses
$ javac src/GcNamesDemo.java && java -XX:+UseZGC -XX:+ZGenerational GcNamesDemo (21.0.12.1)
gc bean : ZGC Minor Cycles
gc bean : ZGC Minor Pauses
gc bean : ZGC Major Cycles
gc bean : ZGC Major Pauses
+6
View File
@@ -0,0 +1,6 @@
$ javac --add-modules jdk.incubator.vector src/VectorDemo.java && java --add-modules jdk.incubator.vector VectorDemo (21.0.12.1)
warning: using incubating module(s): jdk.incubator.vector
1 warning
WARNING: Using incubator modules: jdk.incubator.vector
vector == scalar: true
lanes >= 2 : true
@@ -0,0 +1,2 @@
$ javac --release 21 --enable-preview src/StringTemplatesPreview.java && java --enable-preview StringTemplatesPreview (21.0.12.1)
Hello Ankur, 3 + 4 = 7
@@ -0,0 +1,4 @@
$ javac --release 21 --enable-preview src/UnnamedPreview.java && java --enable-preview UnnamedPreview (21.0.12.1)
x only : 5
loop count : 3
caught, name not needed
@@ -0,0 +1,2 @@
$ java --enable-preview --source 21 src/UnnamedMainPreview.java (21.0.12.1)
no class, no static, no String[] args
@@ -0,0 +1,3 @@
$ javac --release 21 --enable-preview src/ScopedValuesPreview.java && java --enable-preview ScopedValuesPreview (21.0.12.1)
inside handler : user=ankur
outside : bound=false
@@ -0,0 +1,3 @@
$ javac --release 21 --enable-preview src/StructuredConcurrencyPreview.java && java --enable-preview StructuredConcurrencyPreview (21.0.12.1)
both subtasks : ankur / 42
failure shuts down siblings: java.lang.IllegalStateException: boom
+2
View File
@@ -0,0 +1,2 @@
$ javac --release 21 --enable-preview src/FfmPreview.java && java --enable-preview --enable-native-access=ALL-UNNAMED FfmPreview (21.0.12.1)
strlen("Hello Ankur") = 11
@@ -0,0 +1,18 @@
$ javac broken/PreviewWithoutFlag.java (21.0.12.1)
broken/PreviewWithoutFlag.java:3: error: string templates are a preview feature and are disabled by default.
public static void main(String[] args) { String n = "x"; System.out.println(STR."hi \{n}"); }
^
(use --enable-preview to enable string templates)
1 error
$ javac broken/NonExhaustive.java (21.0.12.1)
broken/NonExhaustive.java:6: error: the switch expression does not cover all possible input values
static String f(S s) { return switch (s) { case A a -> "a"; }; }
^
1 error
$ javac broken/DominatedCase.java (21.0.12.1)
broken/DominatedCase.java:3: error: this case label is dominated by a preceding case label
static String f(Object o) { return switch (o) { case CharSequence cs -> "cs"; case String s -> "s"; default -> "d"; }; }
^
1 error