Add java17-features module: runnable demos and captured output for the ankurm.com java17-features article
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
/** Pattern matching for instanceof (final in 16, JEP 394) and Stream.toList() (16). */
|
||||
public class PatternInstanceofDemo {
|
||||
static String describe(Object o) {
|
||||
if (o instanceof String s && s.length() > 3) return "long string " + s;
|
||||
if (o instanceof Integer i) return "int " + (i + 1);
|
||||
return "other " + o;
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
System.out.println(describe("hello"));
|
||||
System.out.println(describe("hi"));
|
||||
System.out.println(describe(41));
|
||||
var list = java.util.stream.Stream.of(3, 1, 2).sorted().toList();
|
||||
System.out.println("Stream.toList : " + list);
|
||||
try { list.add(4); } catch (UnsupportedOperationException e) { System.out.println("toList() result is unmodifiable"); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user