JDK 11 一页速查卡
版本与边界
3先确认 class 版本
java -version
javac -version
javap -verbose App | grep major
JDK 8/9/10/11 对应 52/53/54/55。class 版本高于运行时,只会抛 UnsupportedClassVersionError。
9 / 10 / 11 各带来了什么
- 9:
List.of/Set.of/Map.of、JPMS、JShell - 10:
var、List.copyOf、Collectors.toUnmodifiable* - 11:HttpClient、String 新方法、
Files.readString/writeString、单文件启动、ZGC / Epsilon(实验)
11 是 LTS,但不是所有常用写法都来自 11;面试最常考的就是把 9/10 的特性算到 11 头上。
被移除与被弃用的
- 移除:Java EE / CORBA 模块(JAXB、JAX-WS、activation、CORBA)
- 保留:
java.transaction.xa未被移除,仍是独立模块 - 弃用:Nashorn(15 移除)、Pack200(14 移除)、CMS(14 移除)
JAXB / JAX-WS 需要引入外部实现;不要再假设 javax.xml.bind 在 JDK 11 里直接可用。
语言与核心 API
3局部变量推断
var list = new ArrayList<String>();
var anon = new Object() {
String hi() { return "hi"; }
};
System.out.println(anon.hi());
var 本身是 10(JEP 286);lambda 参数是 11 的扩展(JEP 323)。不能用于字段、方法参数、返回值、catch。var x = null 不合法。
11 的四个常用方法
" ".isBlank(); // true
" a ".strip(); // "a"
"a\nb".lines().count(); // 2
"ab".repeat(2); // "abab"
strip 按 Unicode 空白判断,trim 只去掉 ≤ U+0020;lines 末尾换行不产生额外空行;repeat 次数为负抛 IAE。
读写字符串与集合补丁
Files.readString(Path.of("a.txt"));
Files.writeString(Path.of("b.txt"), "hi");
List.copyOf(src); // JDK 10
Optional.empty().isEmpty(); // JDK 11
readString/writeString 默认 UTF-8;大文件用 lines() 或 newBufferedReader。List.copyOf 是 10,不是 11。
HTTP 与运行时
3同步请求
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(2))
.build();
HttpRequest req = HttpRequest.newBuilder(URI.create(url))
.timeout(Duration.ofSeconds(5))
.GET()
.build();
HttpResponse<String> res = client.send(
req, HttpResponse.BodyHandlers.ofString());
HttpClient 线程安全、可复用;11 中不是 AutoCloseable。默认不跟随重定向,需要 followRedirects。
CompletableFuture 管线
client.sendAsync(req, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
send 与 sendAsync 共用连接池与协议管线;异常在 cause 里,超时是 HttpTimeoutException。join 抛 CompletionException。
G1 / ZGC / Epsilon
java -XX:+UseG1GC -Xmx4g App # 11 默认
java -XX:+UnlockExperimentalVMOptions \
-XX:+UseZGC -Xmx16g App # 11 实验,仅 Linux/x64
java -XX:+UnlockExperimentalVMOptions \
-XX:+UseEpsilonGC App # 不回收
ZGC 11 单代、无压缩 oops、不支持类卸载;15 转正,21 分代。Epsilon 只适合测试。
模块与迁移
3requires / exports / opens
module com.example.app {
requires com.example.lib;
exports com.example.app;
opens com.example.app to com.example.tool;
}
requires 缺模块启动即失败;exports 暴露 public 类型;opens 才允许深反射。requires transitive 把读权限传给下游。
模块路径与类路径
java --module-path mods -m com.example.app/com.example.app.Main
java --module-path mods --add-modules com.example.lib -cp app.jar Main
模块路径上的模块不会自动全部解析;类路径是一个未命名模块,只能读已解析的模块。缺模块时先查 --add-modules。
跨版本编译
javac --release 11 -d out App.java
javac --release 8 -d out8 App.java
--release 用 ct.sym 限制可用 API,能避免编译到目标 JDK 不存在的 API;-source/-target 只改 class 版本,不限制 API。11 的 javac 支持 6–11。
4 个最容易记错的点TRAPS
它是编译期推断,类型一旦确定就固定;不能用于字段、方法参数、返回值、catch,也不能写 var x = null。lambda 参数用 var 时,所有参数必须同时写或同时不写。
11 中不是 AutoCloseable;默认不跟随重定向(NEVER);HTTP_2 只是偏好,实际协议看 response.version()。只设 connectTimeout 不覆盖响应等待。
List.of / Set.of / Map.of 与 List.copyOf 都拒绝 null;Arrays.asList 允许 null。Set.of / Map.of 的重复元素或键抛 IAE,Map.of 最多 10 对。
JDK 9–15 默认 --illegal-access=permit,只警告;16 默认 deny;17 移除该选项。ZGC 11 是实验特性且仅 Linux/x64,15 才转正。
继续深入NEXT
内容基于本机 JDK 11.0.28 的编译/运行结果;HTTP 使用本地 HttpServer,ZGC 仅验证了 Windows 上的启动拒绝。