-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava21.java
More file actions
59 lines (52 loc) · 2.12 KB
/
java21.java
File metadata and controls
59 lines (52 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package hopoz.newFeaturesofJava_test;
import java.util.LinkedHashSet;
import java.util.SequencedCollection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class java21 {
public static void main(String[] args) {
// EXP 字符串模板(preview)
String name = "Java21";
// System.out.println(STR.`Hello, \{name}!`); vscode java不支持开启预览功能
// EXP 序列化集合
SequencedCollection<String> seq = new LinkedHashSet<>();
seq.add("a");
// EXP 分代ZGC 默认关闭
// java -XX:+UseZGC -XX:+ZGenerational ...
// EXP 记录模式
record Shape(String type, long unit) {
}
// 之前
Shape circle = new Shape("Circle", 10);
if (circle instanceof Shape shape) {
System.out.println("Area of " + shape.type() + " is : " + Math.PI * Math.pow(shape.unit(), 2));
}
// 使用记录模式后
circle = new Shape("Circle", 10);
if (circle instanceof Shape(String type, long unit)) {
System.out.println("Area of " + type + " is : " + Math.PI * Math.pow(unit, 2));
}
// EXP 外部函数和内存 API(第三次预览)
// EXP 未命名模式和变量(预览)
// EXP 虚拟线程
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 10; i++) {
int taskId = i;
executor.submit(() -> {
System.out.println("Running task " + taskId + " in virtual thread: " + Thread.currentThread());
});
}
}
// EXP 未命名类和实例 main 方法 (预览)
}
// EXP switch 模式匹配
static String formatterPatternSwitch(Object obj) {
return switch (obj) {
case Integer i -> String.format("int %d", i);
case Long l -> String.format("long %d", l);
case Double d -> String.format("double %f", d);
case String s -> String.format("String %s", s);
default -> obj.toString();
};
}
}