
此版本是 Java SE 平台版本 23 的参考实现,由 Java Community Process 中的 JSR 398 指定。JDK 23 于 2024 年 9 月 17 日正式发布,Oracle 提供了 GPL 协议下的生产可以的构建产。作为一个非 LTS 版本,整体变化不大,从 Features 中看到,大多数都是 Preview,并且相当部分是在此前版本已经处于 Preview 状态的延续。正式 GA 的主要包括:
下面是完整的 Features 列表:
455: | Primitive Types in Patterns, instanceof, and switch (Preview) Patterns、instanceof 和 switch 中的基元类型(预览版) |
|---|---|
466: | Class-File API (Second Preview) 类文件 API(第二次预览版) |
467: | Markdown Documentation Comments Markdown 文档评论 |
469: | Vector API (Eighth Incubator) Vector API(第八个培养箱) |
473: | Stream Gatherers (Second Preview) 流收集器(第二次预览) |
471: | Deprecate the Memory-Access Methods in sun.misc.Unsafe for Removal 废弃 sun.misc.Unsafe 中的 memory-access 方法,以便删除 |
474: | ZGC: Generational Mode by Default ZGC:默认为分代模式 |
476: | Module Import Declarations (Preview) 模块导入声明(预览版) |
477: | Implicitly Declared Classes and Instance Main Methods (Third Preview) 隐式声明的类和实例 main 方法(第三次预览) |
480: | Structured Concurrency (Third Preview) 结构化并发(第三预览版) |
481: | Scoped Values (Third Preview) 限定范围的值(第三个预览版) |
482: | Flexible Constructor Bodies (Second Preview) 灵活的构造函数主体(第二预览版) |
迭代时间表
JEP 455 通过在所有上下文中支持原始类型模式(包括 instanceof 和 switch 语句),增强了 Java 的模式匹配功能。该特性扩展了模式匹配的适用范围,不再局限于引用类型,还包括所有基本类型。其目的是统一数据处理方式,使类型模式与 instanceof 更加一致,并让 switch 可以处理任意的原始类型值。此改进提高了 Java 语言的表达力和一致性,使得在处理基本类型时,无需手动进行类型检查和强制转换。
void processValue(Object value) {
switch (value) {
case Integer i when i > -> System.out.println("Large integer: " + i);
case int i -> System.out.println("Small integer: " + i);
case Double d when d > -> System.out.println("Positive double: " + d);
case double d -> System.out.println("Non-positive double: " + d);
case Long l -> System.out.println("Long value: " + l);
case Boolean b -> System.out.println("Boolean value: " + b);
default -> System.out.println("Unsupported type");
}
}在这个示例中展示了如何在 switch 语句中使用原始类型模式来处理多种类型,包括装箱类型和未装箱的基本类型。代码演示了对不同基本类型进行类型检查、值提取和条件匹配(通过 when 子句),体现了 JEP 455 所带来的增强表达能力和灵活性。
Java 23 在文档注释中引入了对 Markdown 的支持,这个点就是 jdk 官方赶潮流的一种表现。这个增强功能允许直接在代码中提供更富有表现力和可读性的文档。
/**
* # Calculator Class
*
* This class provides basic arithmetic operations.
*
* ## Methods
*
* - `add(int a, int b)`: Returns the sum of two integers
* - `subtract(int a, int b)`: Returns the difference between two integers
*
* ## Example Usage
*
* java
* Calculator calc = new Calculator();
* int sum = calc.add(5, 3); // Returns 8
*
*
* @author Java Developer
* @version 1.0
*/
public class Calculator {
// Class implementation...
}类文件 API 现在处于第二个预览版阶段,它提供了一种解析、生成和转换 Java 类文件的标准方法。此 API 旨在与类文件格式一起发展,后续作为替换 JDK 的 ASM 库的一个标准能力。
ClassFile classFile = ClassFile.read(inputStream);
Method method = classFile.methods().stream()
.filter(m -> m.name().equals("main"))
.findFirst()
.orElse(null);Stream API 的这一增强功能支持自定义中间操作,使流管道更加灵活和富有表现力。如果你想将 Gatherer 用于使用现有 Stream API 方法不容易实现的更复杂的操作,则可以创建自定义 Gatherer。
List<Integer> numbers = List.of(, , , , , , , , , );
List<List<Integer>> pairedEvenSquares = numbers.stream()
.filter(n -> n % == )
.map(n -> n * n)
.gather(Gatherers.windowFixed())
.toList();
System.out.println(pairedEvenSquares); // Output: [[4, 16], [36, 64], [100]]怎么说呢, JAVA 的发展肯定是要将其门槛进一步降低,使得其可以和 python 等语言一样,从语言上变得更加简洁和灵活。放弃 psvm(public static void main) 的 JDK 23 更有人情味了...
// 当前
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}现在
void main() {
println("Hello, World!");
}其他还有诸如结构化并发等其他持续进行的预览功能,这里就不再一一列举;随着 Java 的不断发展,作为开发人员必须及时了解这些变化。无论是在构建企业应用程序、从事开源项目、还是只是学习语言,了解这些新功能都可以帮助我们编写更具酷、更高效和可维护性更高的代码。