Java语言特性系列 • Java5的新特性[1] • Java6的新特性[2] • Java7的新特性[3] • Java8的新特性[4] • Java9的新特性[5] • Java10的新特性[6] • Java11的新特性[7] • Java12的新特性[8] • Java13的新特性[9] • Java14的新特性[10] • Java15的新特性[11] • Java16的新特性[12] • Java17的新特性[13] • Java18的新特性[14] • Java19的新特性[15]
序
本文主要讲述一下Java18的新特性
版本号
代码语言:javascript复制java -version
openjdk version "18" 2022-03-22
OpenJDK Runtime Environment (build 18 36-2087)
OpenJDK 64-Bit Server VM (build 18 36-2087, mixed mode, sharing)
从version信息可以看出是build 18 36
特性列表
JEP 400: UTF-8 by Default[16]
java18以前Charset.defaultCharset()是根据操作系统、user locale等来决定的,导致不同操作系统的默认charset是不一样,这次统一改为了UTF-8 java18要统一为UTF-8则需要
-Dfile.encoding=UTF-8
来设置 如果还想沿用以前的判断方式则可以通过-Dfile.encoding=COMPAT
来设置
JEP 408: Simple Web Server[17]
代码语言:javascript复制提供了一个类似python的SimpleHTTPServer(
python -m SimpleHTTPServer [port]
)的开箱即用的HTTP文件服务器 可以通过jwebserver -p 9000
启动
jwebserver -p 9000
Binding to loopback by default. For all interfaces use "-b 0.0.0.0" or "-b ::".
Serving /tmp and subdirectories on 127.0.0.1 port 9000
URL http://127.0.0.1:9000/
代码语言:javascript复制也可以在代码里定制并启动
jshell> var server = SimpleFileServer.createFileServer(new InetSocketAddress(8080),
...> Path.of("/some/path"), OutputLevel.VERBOSE);
jshell> server.start()
JEP 413: Code Snippets in Java API Documentation[18]
代码语言:javascript复制以前要在通过javadoc展示代码可以使用@code如下
{@code
lines of source code
}
代码语言:javascript复制>但是它的缺点就是得用pre包装,导致该片段不能包含html标签,而且缩进不太灵活 >而这次给javaDoc引入了@snippet标签,无需对html标签再进行转义
/**
• The following code shows how to use {@code Optional.isPresent}:
• {@snippet :
• if (v.isPresent()) {
• System.out.println("v: " v.get());
• }
• }
• /
代码语言:javascript复制也可以直接引用源代码,避免javadoc的代码与实际代码脱节
/**
* The following code shows how to use {@code Optional.isPresent}:
* {@snippet file="ShowOptional.java" region="example"}
*/
JEP 416: Reimplement Core Reflection with Method Handles[19]
通过Method Handles重新实现java.lang.reflect.Method, Constructor及Field来替代字节码生成的Method::invoke, Constructor::newInstance, Field::get, and Field::set的实现 方便支持Project Valhalla,为以后减少扩展成本
JEP 417: Vector API (Third Incubator)[20]
JDK16引入了JEP 338: Vector API (Incubator)[21]提供了jdk.incubator.vector来用于矢量计算 JDK17进行改进并作为第二轮的incubatorJEP 414: Vector API (Second Incubator)[22] JDK18进行改进并作为第三轮的incubator
JEP 418: Internet-Address Resolution SPI[23]
给解析网络地址提供了SPI,即java.net.spi包的InetAddressResolverProvider 方便给project loom做准备(目前InetAddress的API会阻塞在系统调用),也方便定制化及testing
JEP 419: Foreign Function & Memory API (Second Incubator)[24]
JDK14的JEP 370: Foreign-Memory Access API (Incubator)[25]引入了Foreign-Memory Access API作为incubator JDK15的JEP 383: Foreign-Memory Access API (Second Incubator)[26]Foreign-Memory Access API作为第二轮incubator JDK16的JEP 393: Foreign-Memory Access API (Third Incubator)[27]作为第三轮,它引入了Foreign Linker API JDK17引入JEP 412: Foreign Function & Memory API (Incubator)[28]作为第一轮incubator JDK18则作为第二轮的incubator
JEP 420: Pattern Matching for switch (Second Preview)[29]
代码语言:javascript复制instanceof的模式匹配在JDK14作为preview,在JDK15作为第二轮的preview,在JDK16转正
static String formatterPatternSwitch(Object o) {
return switch (o) {
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 -> o.toString();
};
}
JDK17引入JEP 406: Pattern Matching for switch (Preview)[30] JDK18则作为第二轮的preview
JEP 421: Deprecate Finalization for Removal[31]
废弃finalize方法方便后续移除
细项解读
上面列出的是大方面的特性,除此之外还有一些api的更新及废弃,主要见JDK 18 Release Notes[32],这里举几个例子。
添加项
•
SerialGC、ParallelGC、ZGC支持String Deduplication
可使用
-XX: UseStringDeduplication
开启
•
Map from an Element to its JavaFileObject
新增
Elements.getFileObjectOf(Element)
来映射为JavaFileObject
•
Configurable Card Table Card SizeJDK-8272773[33]
可以使用
-XX:GCCardSizeInBytes
来设置card table大小
•
Allow G1 Heap Regions up to 512MBJDK-8275056[34]
允许G1的heap regions的最大值从之前的32MB到512MB
•
JDK Flight Recorder Event for FinalizationJDK-8266936[35]
新增jdk.FinalizerStatistics
移除项
•
Removal of Google's GlobalSign Root CertificateJDK-8225083[36]
移除了google的GlobalSign根证书
•
Removal of Empty finalize() Methods in java.desktop ModuleJDK-8273102[37]
移除java.desktop模块里头的空finalize()方法
•
Removal of impl.prefix JDK System Property Usage From InetAddressJDK-8274227[38]
移除impl.prefix属性,转而使用InetAddressResolver这个spi
•
Removal of Support for Pre JDK 1.4 DatagramSocketImpl ImplementationsJDK-8260428[39]
移除jdk1.4之前的DatagramSocketImpl
•
Removal of Legacy PlainSocketImpl and PlainDatagramSocketImpl ImplementationsJDK-8253119[40]
移除java.net.SocketImpl及java.net.DatagramSocketImpl的老实现PlainSocketImpl、PlainDatagramSocketImpl
jdk.net.usePlainDatagramSocketImpl
属性也一并移除
废弃项
完整列表见deprecated-list[41]
•
Deprecated Subject::doAs for RemovalJDK-8267108[42]
废弃
javax.security.auth.Subject::doAs
为移除做准备
•
Deprecated sun.misc.Unsafe Methods That Return OffsetsJDK-8277863[43]
sun.misc.Unsafe中objectFieldOffset, staticFieldOffset, staticFieldBase方法被废弃
•
Terminally Deprecated Thread.stopJDK-8277861[44]
废弃Thread.stop为后续移除做准备
•
Obsoleted Product Options -XX:G1RSetRegionEntries and -XX:G1RSetSparseRegionEntriesJDK-8017163[45]
废弃
-XX:G1RSetRegionEntries
及-XX:G1RSetSparseRegionEntries
已知问题
• Extended Delay Before JDK Executable Installer Starts From Network DriveJDK-8274002[46]
在 Windows 11 和 Windows Server 2022 上,从映射的网络驱动器启动时,临时安装文件的提取可能会有些缓慢。安装程序仍然可以工作,但可能会有暂时的延迟。
小结
Java18主要有如下几个特性
• JEP 400: UTF-8 by Default[47] • JEP 408: Simple Web Server[48] • JEP 413: Code Snippets in Java API Documentation[49] • JEP 416: Reimplement Core Reflection with Method Handles[50] • JEP 417: Vector API (Third Incubator)[51] • JEP 418: Internet-Address Resolution SPI[52] • JEP 419: Foreign Function & Memory API (Second Incubator)[53] • JEP 420: Pattern Matching for switch (Second Preview)[54] • JEP 421: Deprecate Finalization for Removal[55]
doc
• JDK 18 Features[56] • JDK 18 Release Notes[57] • Consolidated JDK 18 Release Notes[58] • Java SE 18 deprecated-list[59] • The Arrival of Java 18[60] • JDK 18 G1/Parallel/Serial GC changes[61]