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

This commit is contained in:
2026-10-20 17:13:58 +00:00
parent 8b9447a393
commit 5a076c826f
23 changed files with 261 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
$ javac src/RecordsDemo.java && java RecordsDemo (17.0.20.1)
toString : Point[x=3, y=4]
equals : true hashCode equal: true
accessor : p.x() = 3, distance = 5.0
compact ctor : negative coordinate: -1,2
shallow : Team[name=core, members=[a, b]] <- the record's list changed after construction
+6
View File
@@ -0,0 +1,6 @@
$ javac src/SealedDemo.java && java SealedDemo (17.0.20.1)
Circle[r=1.0] area = 3.142
Square[side=2.0] area = 4.000
Rect[w=2.0, h=3.0] area = 6.000
permitted : [Circle, Square, Rect]
isSealed : true
@@ -0,0 +1,8 @@
$ javac src/TextBlocksDemo.java && java TextBlocksDemo (17.0.20.1)
{
"name": "Ankur",
"city": "Mumbai"
}
lines : 4
backslash-newline: joined on one line
formatted : Hello, reader!
@@ -0,0 +1,5 @@
$ javac src/SwitchExprDemo.java && java SwitchExprDemo (17.0.20.1)
MON -> 9 working hours
SAT -> 0 working hours
WED -> 8 working hours
old fall-through: one two
@@ -0,0 +1,6 @@
$ javac src/PatternInstanceofDemo.java && java PatternInstanceofDemo (17.0.20.1)
long string hello
other hi
int 42
Stream.toList : [1, 2, 3]
toList() result is unmodifiable
@@ -0,0 +1,2 @@
$ javac src/HelpfulNpeDemo.java && java HelpfulNpeDemo (17.0.20.1)
Cannot read field "city" because "u.address" is null
@@ -0,0 +1,13 @@
$ javac src/EncapsulationDemo.java && java EncapsulationDemo (11.0.32.1)
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by EncapsulationDemo (file:/…/) to field java.lang.String.value
WARNING: Please consider reporting this to the maintainers of EncapsulationDemo
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
read String.value: byte[]
$ javac src/EncapsulationDemo.java && java EncapsulationDemo (17.0.20.1)
java.lang.reflect.InaccessibleObjectException: Unable to make field private final byte[] java.lang.String.value accessible: module java.base does not "opens java.lang" to unnamed module @<hash>
$ java --add-opens java.base/java.lang=ALL-UNNAMED EncapsulationDemo (17.0.20.1)
read String.value: byte[]
@@ -0,0 +1,4 @@
$ javac src/RandomDemo.java && java RandomDemo (17.0.20.1)
L64X128MixRandom nextInt(100) x3: 98 1 80
ThreadLocalRandom implements RandomGenerator: true
algorithms >= 10: true
@@ -0,0 +1,18 @@
$ javac broken/SealedViolation.java (17.0.20.1)
broken/SealedViolation.java:5: error: class is not allowed to extend sealed class: Shape (as it is not listed in its permits clause)
record Triangle(double b, double h) implements Shape {}
^
1 error
$ javac broken/RecordFieldMutation.java (17.0.20.1)
broken/RecordFieldMutation.java:3: error: cannot assign a value to final variable x
record Point(int x, int y) { void move() { this.x = 10; } }
^
1 error
$ javac broken/SwitchPatternOn17.java (17.0.20.1)
broken/SwitchPatternOn17.java:3: error: patterns in switch statements are a preview feature and are disabled by default.
static String f(Object o) { return switch (o) { case String s -> "string"; default -> "other"; }; }
^
(use --enable-preview to enable patterns in switch statements)
1 error