Java language feature series
- New features of Java5
- New features of Java6
- New features of Java7
- New features of Java8
- New features of Java9
- What's new in Java 10
- What's new in Java 11
- New features in Java 12
- New features in Java 13
- New features in Java 14
- New features in Java 15
- New features in Java 16
- New features in Java 17
- New features in Java 18
- New features in Java 19
- New features of Java 20
sequence
This article mainly describes the new features of Java 18
version number
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)
It can be seen from the version information that it is build 18+36
Feature list
JEP 400: UTF-8 by Default
Before java18, Charset.defaultCharset() was determined according to the operating system, user locale, etc., resulting in different default charsets for different operating systems. This time, it was changed to UTF-8.
To unify java18 into UTF-8, you need-Dfile.encoding=UTF-8
to set it. If you want to continue to use the previous judgment method, you can set it through-Dfile.encoding=COMPAT
JEP 408: Simple Web Server
Provides an out-of-the-box HTTP file server like Python's SimpleHTTPServer (python -m SimpleHTTPServer [port]
) that can be started viajwebserver -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/
It can also be customized and started in the code
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
In the past, to show code through javadoc, you can use @code as follows
<pre>{@code
lines of source code
}</pre>
But its disadvantage is that it has to be wrapped with pre, resulting in that the fragment cannot contain html tags, and the indentation is not very flexible
This time, the @snippet tag is introduced to javaDoc, so there is no need to escape the html tag.
/**
* The following code shows how to use {@code Optional.isPresent}:
* {@snippet :
* if (v.isPresent()) {
* System.out.println("v: " + v.get());
* }
* }
*/
You can also directly refer to the source code to avoid the disconnection between the javadoc code and the actual code
/**
* The following code shows how to use {@code Optional.isPresent}:
* {@snippet file="ShowOptional.java" region="example"}
*/
JEP 416: Reimplement Core Reflection with Method Handles
Reimplement java.lang.reflect.Method, Constructor and Field through Method Handles to replace the implementation of Method::invoke, Constructor::newInstance, Field::get, and Field::set generated by bytecode to support Project Valhalla, Reduce expansion costs for future
JEP 417: Vector API (Third Incubator)
JDK16 introduced JEP 338: Vector API (Incubator) provides jdk.incubator.vector for vector computation
JDK17 improves and serves as the second round of incubator JEP 414: Vector API (Second Incubator)
JDK18 improves and acts as a third round incubator
JEP 418: Internet-Address Resolution SPI
Provides SPI for parsing network addresses, namely the InetAddressResolverProvider of the java.net.spi package
It is convenient to prepare for project loom (currently, the API of InetAddress will block the system call), and it is also convenient for customization and testing
JEP 419: Foreign Function & Memory API (Second Incubator)
JEP 370: Foreign-Memory Access API (Incubator) of JDK14 introduces the Foreign-Memory Access API as an incubator
JDK15's JEP 383: Foreign-Memory Access API (Second Incubator) Foreign-Memory Access API as a second-round incubator
JDK16's JEP 393: Foreign-Memory Access API (Third Incubator) as the third round, it introduces the Foreign Linker API
JDK17 introduces JEP 412: Foreign Function & Memory API (Incubator) as the first round of incubator
JDK18 is used as the second round of incubator
JEP 420: Pattern Matching for switch (Second Preview)
The pattern matching of instanceof is used as a preview in JDK14, as a second-round preview in JDK15, and positive in 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 introduces JEP 406: Pattern Matching for switch (Preview)
JDK18 is used as a preview of the second round
JEP 421: Deprecate Finalization for Removal
Abandon the finalize method to facilitate subsequent removal
Detailed Interpretation
Listed above are major features, in addition to some api updates and discards, mainly see JDK 18 Release Notes , here are a few examples.
Add item
SerialGC, ParallelGC, ZGC support String Deduplication
Available
-XX:+UseStringDeduplication
OpenMap from an Element to its JavaFileObject
Added
Elements.getFileObjectOf(Element)
to map to JavaFileObjectConfigurable Card Table Card Size JDK-8272773
You can use
-XX:GCCardSizeInBytes
to set the card table sizeAllow G1 Heap Regions up to 512MB JDK-8275056
The maximum allowed heap regions for G1 is from the previous 32MB to 512MB
JDK Flight Recorder Event for Finalization JDK-8266936
Added jdk.FinalizerStatistics
remove item
Removal of Google's GlobalSign Root Certificate JDK-8225083
Removed google's GlobalSign root certificate
Removal of Empty finalize() Methods in java.desktop Module JDK-8273102
Remove the empty finalize() method in the java.desktop module
Removal of impl.prefix JDK System Property Usage From InetAddress JDK-8274227
Remove the impl.prefix property and use the spi of InetAddressResolver instead
Removal of Support for Pre JDK 1.4 DatagramSocketImpl Implementations JDK-8260428
Remove DatagramSocketImpl before jdk1.4
Removal of Legacy PlainSocketImpl and PlainDatagramSocketImpl Implementations JDK-8253119
Removed the old implementations of java.net.SocketImpl and java.net.DatagramSocketImpl PlainSocketImpl, PlainDatagramSocketImpl
jdk.net.usePlainDatagramSocketImpl
attributes are also removed
obsolete
See deprecated-list for the full list
Deprecated Subject::doAs for Removal JDK-8267108
javax.security.auth.Subject::doAs
Prepare for removalDeprecated sun.misc.Unsafe Methods That Return Offsets JDK-8277863
objectFieldOffset, staticFieldOffset, staticFieldBase methods in sun.misc.Unsafe are abandoned
Terminally Deprecated Thread.stop JDK-8277861
Deprecated Thread.stop in preparation for subsequent removal
Obsoleted Product Options -XX:G1RSetRegionEntries and -XX:G1RSetSparseRegionEntries JDK-8017163
Obsolete
-XX:G1RSetRegionEntries
and-XX:G1RSetSparseRegionEntries
Known Issues
Extended Delay Before JDK Executable Installer Starts From Network Drive JDK-8274002
On Windows 11 and Windows Server 2022, the extraction of temporary installation files may be somewhat slow when booting from a mapped network drive. The installer will still work, but there may be a temporary delay.
summary
Java18 mainly has the following features
- JEP 400: UTF-8 by Default
- JEP 408: Simple Web Server
- JEP 413: Code Snippets in Java API Documentation
- JEP 416: Reimplement Core Reflection with Method Handles
- JEP 417: Vector API (Third Incubator)
- JEP 418: Internet-Address Resolution SPI
- JEP 419: Foreign Function & Memory API (Second Incubator)
- JEP 420: Pattern Matching for switch (Second Preview)
- JEP 421: Deprecate Finalization for Removal
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。