Java 8 到 21 的主要新特性及示例代码

版本 年份 主要新特性
8 2014 Lambda表达式、Stream API、接口默认方法、方法引用、Optional类、新的日期时间API、Nashorn引擎
9 2017 模块系统、接口私有方法、集合工厂方法、JShell、响应式流、HTTP/2客户端(孵化)
10 2018 局部变量类型推断(var)、应用类数据共享、并行全GC(G1)
11 2018 HTTP客户端(标准)、字符串增强方法、单文件运行、var在Lambda中、Flight Recorder开源
12 2019 Switch表达式(预览)、JVM常量API、Shenandoah GC(实验)
13 2019 文本块(预览)、增强Switch表达式、动态CDS归档
14 2020 instanceof模式匹配(预览)、Records(预览)、有用的NPE信息、Switch表达式(正式)
15 2020 文本块(正式)、Sealed类(预览)、隐藏类、ZGC正式
16 2021 Records(正式)、模式匹配instanceof(正式)、Stream.toList()、Vector API(孵化)
17 2021 Sealed类(正式)、Switch模式匹配(预览)、移除Applet API
18 2022 UTF-8默认字符集、简单Web服务器、Javadoc代码片段(@snippet
19 2022 虚拟线程(预览)、Record模式(预览)、结构化并发(孵化)、外部函数API(预览)
20 2023 虚拟线程(第二次预览)、Scoped Values(孵化)、Record模式(第二次预览)
21 2023 虚拟线程(正式)、字符串模板(预览)、分代ZGC、Record模式(正式)、Switch模式匹配(正式)

示例代码

Java 8

// Lambda表达式
Runnable r = () -> System.out.println("Hello");

// Stream API
List<String> list = List.of("a", "b", "c");
list.stream().filter(s -> s.startsWith("a")).forEach(System.out::println);

// 接口默认方法
interface MyInterface {
    default void log() {
        System.out.println("Default method");
    }
}

// Optional类
Optional<String> optional = Optional.ofNullable(null);
optional.ifPresentOrElse(System.out::println, () -> System.out.println("Empty"));

Java 9

// 模块系统(module-info.java)
module com.example {
    requires java.base;
    exports com.example;
}

// 集合工厂方法
List<String> list = List.of("a", "b", "c");

// 接口私有方法
interface MyInterface {
    private void helper() {
        System.out.println("Private method");
    }
}

Java 10

// 局部变量类型推断(var)
var list = new ArrayList<String>();
var map = Map.of("key", 42);

Java 11

// HTTP客户端(同步请求)
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("https://example.com")).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());

Java 14

// instanceof模式匹配
if (obj instanceof String s) {
    System.out.println(s.length());
}

// Records(预览)
record Point(int x, int y) {}
Point p = new Point(1, 2);
System.out.println(p.x());

Java 17

// Sealed类
public sealed class Shape permits Circle, Square {}
public final class Circle extends Shape {}
public final class Square extends Shape {}

Java 19

// 虚拟线程(预览)
Thread.startVirtualThread(() -> System.out.println("Running in virtual thread"));

// Record模式(预览)
record Point(int x, int y) {}
if (obj instanceof Point(int x, int y)) {
    System.out.println(x + y);
}

Java 21

// 字符串模板(预览)
String name = "Java";
String message = STR."Hello \{name}!"; // 输出 "Hello Java!"

// 虚拟线程(正式)
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    executor.submit(() -> System.out.println("Virtual thread task"));
}

关键说明

  • 预览特性:部分特性(如文本块、虚拟线程)在早期版本中作为预览功能引入,需通过--enable-preview启用。
  • LTS版本:Java 8、11、17、21为长期支持版本(LTS)。
  • 完整特性:每个版本还包含JVM优化、垃圾收集器改进等底层更新。

如果需要更详细的特性列表或特定版本的示例,请进一步说明!

posted @ 2025-02-13 11:28  little_lunatic  阅读(740)  评论(0)    收藏  举报