Skip to content

Commit eb51143

Browse files
committed
[docs update]完善Java新特性讲解
1 parent 250341c commit eb51143

File tree

4 files changed

+247
-8
lines changed

4 files changed

+247
-8
lines changed

docs/java/new-features/java14-15.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ System.out.println(result);
5656

5757
#### record 关键字
5858

59-
`record` 关键字可以简化 **数据类**(一个 Java 类一旦实例化就不能再修改)的定义方式,使用 `record` 代替 `class` 定义的类,只需要声明属性,就可以在获得属性的访问方法,以及 `toString()``hashCode()`, `equals()`方法
59+
`record` 关键字可以简化 **数据类**(一个 Java 类一旦实例化就不能再修改)的定义方式,使用 `record` 代替 `class` 定义的类,只需要声明属性,就可以在获得属性的访问方法,以及 `toString()``hashCode()`, `equals()`方法
6060

61-
类似于使用 `class` 定义类,同时使用了 lombok 插件,并打上了`@Getter,@ToString,@EqualsAndHashCode`注解
61+
类似于使用 `class` 定义类,同时使用了 lombok 插件,并打上了`@Getter,@ToString,@EqualsAndHashCode`注解
6262

6363
```java
6464
/**

docs/java/new-features/java19.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ tag:
55
- Java新特性
66
---
77

8-
JDK 19 定于 9 月 20 日正式发布以供生产使用,非长期支持版本。不过,JDK 19 中有一些比较重要的新特性值得关注。
8+
JDK 19 定于 2022 年 9 月 20 日正式发布以供生产使用,非长期支持版本。不过,JDK 19 中有一些比较重要的新特性值得关注。
99

1010
JDK 19 只有 7 个新特性:
1111

docs/java/new-features/java20.md

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ JDK 20 只有 7 个新特性:
1818
- [JEP 433:switch 模式匹配](https://openjdk.org/jeps/433)(第四次预览)
1919
- [JEP 434: Foreign Function & Memory API(外部函数和内存 API)](https://openjdk.org/jeps/434)(第二次预览)
2020
- [JEP 436: Virtual Threads(虚拟线程)](https://openjdk.org/jeps/436)(第二次预览)
21-
- [JEP 437: Structured Concurrency(结构化并发)](https://openjdk.org/jeps/437)(第二次孵化)
22-
- [JEP 432向量 API(](https://openjdk.org/jeps/438)第五次孵化)
21+
- [JEP 437:Structured Concurrency(结构化并发)](https://openjdk.org/jeps/437)(第二次孵化)
22+
- [JEP 432:向量 API(](https://openjdk.org/jeps/438)第五次孵化)
2323

2424
## JEP 429:作用域值(第一次孵化)
2525

@@ -38,20 +38,109 @@ ScopedValue.where(V, <value>)
3838

3939
作用域值允许在大型程序中的组件之间安全有效地共享数据,而无需求助于方法参数。
4040

41-
关于作用域值的详细介绍,推荐阅读[作用域值常见问题解答](https://www.happycoders.eu/java/scoped-values/)
41+
关于作用域值的详细介绍,推荐阅读[作用域值常见问题解答](https://www.happycoders.eu/java/scoped-values/)这篇文章
4242

4343
## JEP 432:记录模式(第二次预览)
4444

45-
记录模式(Record Patterns) 可对 record 的值进行解构,可以嵌套记录模式和类型模式,实现强大的、声明性的和可组合的数据导航和处理形式。
45+
记录模式(Record Patterns) 可对 record 的值进行解构,也就是更方便地从记录类(Record Class)中提取数据。并且,还可以嵌套记录模式和类型模式结合使用,以实现强大的、声明性的和可组合的数据导航和处理形式。
4646

4747
记录模式不能单独使用,而是要与 instanceof 或 switch 模式匹配一同使用。
4848

49-
记录模式在 Java 19 进行了第一次预览, 由[JEP 405](https://openjdk.org/jeps/405)提出。JDK 20 中是第二次预览,由 [JEP 432](https://openjdk.org/jeps/432) 提出。这次的改进包括:
49+
先以 instanceof 为例简单演示一下。
50+
51+
简单定义一个记录类:
52+
53+
```java
54+
record Shape(String type, long unit){}
55+
```
56+
57+
没有记录模式之前:
58+
59+
```java
60+
Shape circle = new Shape("Circle", 10);
61+
if (circle instanceof Shape shape) {
62+
63+
System.out.println("Area of " + shape.type() + " is : " + Math.PI * Math.pow(shape.unit(), 2));
64+
}
65+
```
66+
67+
有了记录模式之后:
68+
69+
```java
70+
Shape circle = new Shape("Circle", 10);
71+
if (circle instanceof Shape(String type, long unit)) {
72+
System.out.println("Area of " + type + " is : " + Math.PI * Math.pow(unit, 2));
73+
}
74+
```
75+
76+
再看看记录模式与 switch 的配合使用。
77+
78+
定义一些类:
79+
80+
```java
81+
interface Shape {}
82+
record Circle(double radius) implements Shape { }
83+
record Square(double side) implements Shape { }
84+
record Rectangle(double length, double width) implements Shape { }
85+
```
86+
87+
没有记录模式之前:
88+
89+
```java
90+
Shape shape = new Circle(10);
91+
switch (shape) {
92+
case Circle c:
93+
System.out.println("The shape is Circle with area: " + Math.PI * c.radius() * c.radius());
94+
break;
95+
96+
case Square s:
97+
System.out.println("The shape is Square with area: " + s.side() * s.side());
98+
break;
99+
100+
case Rectangle r:
101+
System.out.println("The shape is Rectangle with area: + " + r.length() * r.width());
102+
break;
103+
104+
default:
105+
System.out.println("Unknown Shape");
106+
break;
107+
}
108+
```
109+
110+
有了记录模式之后:
111+
112+
```java
113+
Shape shape = new Circle(10);
114+
switch(shape) {
115+
116+
case Circle(double radius):
117+
System.out.println("The shape is Circle with area: " + Math.PI * radius * radius);
118+
break;
119+
120+
case Square(double side):
121+
System.out.println("The shape is Square with area: " + side * side);
122+
break;
123+
124+
case Rectangle(double length, double width):
125+
System.out.println("The shape is Rectangle with area: + " + length * width);
126+
break;
127+
128+
default:
129+
System.out.println("Unknown Shape");
130+
break;
131+
}
132+
```
133+
134+
记录模式可以避免不必要的转换,使得代码更建简洁易读。而且,用了记录模式后不必再担心 `null` 或者 `NullPointerException`,代码更安全可靠。
135+
136+
记录模式在 Java 19 进行了第一次预览, 由 [JEP 405](https://openjdk.org/jeps/405) 提出。JDK 20 中是第二次预览,由 [JEP 432](https://openjdk.org/jeps/432) 提出。这次的改进包括:
50137

51138
- 添加对通用记录模式类型参数推断的支持,
52139
- 添加对记录模式的支持以出现在增强语句的标题中`for`
53140
- 删除对命名记录模式的支持。
54141

142+
**注意**:不要把记录模式和 [JDK16](./java16.md) 正式引入的记录类搞混了。
143+
55144
## JEP 433:switch 模式匹配(第四次预览)
56145

57146
正如 `instanceof` 一样, `switch` 也紧跟着增加了类型匹配自动转换功能。

docs/java/new-features/java21.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
---
2+
title: Java 21 新特性概览(重要)
3+
category: Java
4+
tag:
5+
- Java新特性
6+
---
7+
8+
JDK 21 于 2023年9月19日 发布,这是一个非常重要的版本,里程碑式。
9+
10+
JDK21是 LTS(长期支持版),至此为止,目前有 JDK8、JDK11、JDK17和 JDK21这四个长期支持版了。
11+
12+
JDK 21 共有 15 个新特性:
13+
14+
- [JEP 430:String Templates(字符串模板)](https://openjdk.org/jeps/430)(预览)
15+
- [JEP 431:Sequenced Collections(序列化集合)](https://openjdk.org/jeps/431)
16+
17+
- [JEP 439:Generational ZGC(分代ZGC)](https://openjdk.org/jeps/439)
18+
19+
- [JEP 440:Record Patterns(记录模式)](https://openjdk.org/jeps/440)
20+
21+
22+
23+
## JEP 430:字符串模板(预览)
24+
25+
String Templates(字符串模板) 目前仍然是 JDK 21 中的一个预览功能。
26+
27+
String Templates 提供了一种更简洁、更直观的方式来动态构建字符串。通过使用占位符`${}`,我们可以将变量的值直接嵌入到字符串中,而不需要手动处理。在运行时,Java 编译器会将这些占位符替换为实际的变量值。并且,表达式支持局部变量、静态/非静态字段甚至方法、计算结果等特性。
28+
29+
实际上,String Templates(字符串模板)再大多数编程语言中都存在:
30+
31+
```typescript
32+
"Greetings {{ name }}!"; //Angular
33+
`Greetings ${ name }!`; //Typescript
34+
$"Greetings { name }!" //Visual basic
35+
f"Greetings { name }!" //Python
36+
```
37+
38+
Java 在没有 String Templates 之前,我们通常使用字符串拼接或格式化方法来构建字符串:
39+
40+
```java
41+
//concatenation
42+
message = "Greetings " + name + "!";
43+
44+
//String.format()
45+
message = String.format("Greetings %s!", name); //concatenation
46+
47+
//MessageFormat
48+
message = new MessageFormat("Greetings {0}!").format(name);
49+
50+
//StringBuilder
51+
message = new StringBuilder().append("Greetings ").append(name).append("!").toString();
52+
```
53+
54+
这些方法或多或少都存在一些缺点,比如难以阅读、冗长、复杂。
55+
56+
Java 使用 String Templates 进行字符串拼接,可以直接在字符串中嵌入表达式,而无需进行额外的处理:
57+
58+
```java
59+
String message = STR."Greetings \{name}!";
60+
```
61+
62+
在上面的模板表达式中:
63+
64+
- STR 是模板处理器。
65+
- `\{name}`为表达式,运行时,这些表达式将被相应的变量值替换。
66+
67+
Java 目前支持三种模板处理器:
68+
69+
- STR:自动执行字符串插值,即将模板中的每个嵌入式表达式替换为其值(转换为字符串)。
70+
- FMT:和STR类似,但是它还可以接受格式说明符,这些格式说明符出现在嵌入式表达式的左边,用来控制输出的样式
71+
- RAW:不会像STR和FMT模板处理器那样自动处理字符串模板,而是返回一个 `StringTemplate` 对象,这个对象包含了模板中的文本和表达式的信息
72+
73+
```java
74+
String name = "Lokesh";
75+
76+
//STR
77+
String message = STR."Greetings \{name}.";
78+
79+
//FMT
80+
String message = STR."Greetings %-12s\{name}.";
81+
82+
//RAW
83+
StringTemplate st = RAW."Greetings \{name}.";
84+
String message = STR.process(st);
85+
```
86+
87+
除了JDK自带的三种模板处理器外,你还可以实现 `StringTemplate.Processor` 接口来创建自己的模板处理器。
88+
89+
我们可以使用局部变量、静态/非静态字段甚至方法作为嵌入表达式:
90+
91+
```java
92+
//variable
93+
message = STR."Greetings \{name}!";
94+
95+
//method
96+
message = STR."Greetings \{getName()}!";
97+
98+
//field
99+
message = STR."Greetings \{this.name}!";
100+
```
101+
102+
还可以在表达式中执行计算并打印结果:
103+
104+
```java
105+
int x = 10, y = 20;
106+
String s = STR."\{x} + \{y} = \{x + y}"; //"10 + 20 = 30"
107+
```
108+
109+
为了提高可读性,我们可以将嵌入的表达式分成多行:
110+
111+
```java
112+
String time = STR."The current time is \{
113+
//sample comment - current time in HH:mm:ss
114+
DateTimeFormatter
115+
.ofPattern("HH:mm:ss")
116+
.format(LocalTime.now())
117+
}.";
118+
```
119+
120+
## JEP431:序列化集合
121+
122+
JDK 21引入了一种新的集合类型:**Sequenced Collections(序列化集合)**
123+
124+
## JEP 439:分代ZGC
125+
126+
JDK21中对 ZGC 进行了功能扩展,增加了分代 GC 功能。不过,默认是关闭的,需要通过配置打开:
127+
128+
```bash
129+
// 启用分代ZGC
130+
java -XX:+UseZGC -XX:+ZGenerational ...
131+
```
132+
133+
在未来的版本中,官方会把 ZGenerational 设为默认值,即默认打开 ZGC 的分代 GC。在更晚的版本中,非分代ZGC就被移除。
134+
135+
> In a future release we intend to make Generational ZGC the default, at which point -XX:-ZGenerational will select non-generational ZGC. In an even later release we intend to remove non-generational ZGC, at which point the ZGenerational option will become obsolete.
136+
>
137+
> 在将来的版本中,我们打算将Generational ZGC作为默认选项,此时-XX:-ZGenerational将选择非分代ZGC。在更晚的版本中,我们打算移除非分代ZGC,此时ZGenerational选项将变得过时。
138+
139+
分代 ZGC 可以显著减少垃圾回收过程中的停顿时间,并提高应用程序的响应性能。这对于大型 Java 应用程序和高并发场景下的性能优化非常有价值。
140+
141+
## JEP 440:记录模式
142+
143+
记录模式在 Java 19 进行了第一次预览, 由 [JEP 405](https://openjdk.org/jeps/405) 提出。JDK 20 中是第二次预览,由 [JEP 432](https://openjdk.org/jeps/432) 提出。最终,记录模式在 JDK21 顺利转正。
144+
145+
[Java 20 新特性概览](./java20.md)已经详细介绍过记录模式,这里就不重复了。
146+
147+
## 参考
148+
149+
- Java 21 String Templates:<https://howtodoinjava.com/java/java-string-templates/>
150+
-

0 commit comments

Comments
 (0)