Java 9 - Trivias
This article summarises a couple of Java 9 features we did not talk about in our previous articles. As most of them are really rather small, we will go quickly through them - section by section.
Let’s start with enhancements to Project Coin, initially introduced in Java 7.
Project Coin Enhancements
Project Coin introduced rather small language changes in JDK/SE 7. Java 9 addresses some rough edges of those changes:
_as an identifier creates an error now in Java 9 (previously a warning only).
@SafeVarargs(JavaDoc) is now allowed on private instance methods.
- allow effectively-final variables to be used in try-resources (more about that in the next sections).
- usage of the diamond operator with anonymous classes is allowed if the type is denotable.
- support for private methods in interfaces (more about that in the next section).
The try-with-resources language construct, introduced with Java 7, gains another extension in Java 7. You do not have to declare
java.io.Closeable resources within the
try instead you can use an effectively-final variable from within the
You can do that for multiple resources too:
Private methods in interfaces
Private methods are now allowed in interfaces to share common functionality between either static or default method implementations in the interface:
The following features are independent of Project Coin changes addressing other areas in JDK/SE.
Java 9 comes with a nice API addition supporting the implementation of flow-controlled components: the Flow API, found in package
The new interfaces from the Flow API implement the reactive streams specification. The following interfaces come with Java 9:
Flow.Publisher- is a producer of items
Flow.Subscriber- a receiver of items
Flow.Subscription- message control linking between
Flow.Processor- a component which acts as both, as publisher and subscriber
So those classes are basically interfaces only. In JDK 9 itself, concrete interface implementations can be found in the
java.util.concurrent package and the
jdk.incubator.http package. For the new Java 9 HTTP client, there is for example a
HttpResponse.BodyProcessor interface which allows for the subscriber to work on the incoming byte-stream.
HTTP 2 Client
As mentioned in the last section, Java 9 comes with a new HTTP 2 client found in package
jdk.incubator.httpclient in module
jdk.incubator.httpclient. The classes therein define high-level HTTP and WebSocket APIs.
HttpClient.Builder can be used to create an instance of
HttpClient. Those instances are immutable and can be used for multiple
HttpRequests. Besides synchronous sending with
HttpClient also has an asynchronous
sendAsync method returning a
Besides providing a new search box, JavaDoc has also been enhanced to generate a nicer HTML 5 markup.
Enhanced Depreciation is another small feature, being quite useful though. It is about extending the
@Deprecated annotation with new attributes for providing better and more detailed information about the status and the intended disposition of APIs.
@Deprecated(since = "9") allows for specifying the version in which the annotated element became deprecated. The given version string should be in the same format as the version used in the JavaDoc
@Deprecated(forRemoval = true) indicates whether the annotated element is subject to removal in a future version.
Several Java SE APIs have been refined with more detailed
java.lang.ProcessHandle (JavaDoc) has been introduced in Java 9 to get more information about the currently running JVM process and its children. It provides methods for querying the operating system process that the JVM is currently running in.
For example, querying the current process ID (PID) is now as easy as
ProcessHandle::inf() returns a
ProcessHandle.Info instance containing various meta-data about the current process, like the command, command-line arguments, process user, etc.
UTF-8 Property Files
The default encoding for property files changes from ISO-8859-1 to UTF-8 in Java 9. Applications therefore no longer need to convert characters whose code points are over U+00FF to escaped characters. If, for some reason, this change needs to be overridden, there is a new system property
java.util.PropertyResourceBundle.encoding which allows for that.
jlink - Java Linker
jlink is a new tool distributed with the JDK that can assemble and optimise the set of modules into a custom run-time image. We will cover
jlink in a separate blog post, for now you can gather more information here or here.
This article summarises Java 9 features not being covered in our previous articles. Java 9 comes with a couple of nice though smaller features, enhancing Project Coin or adding new Flow reactive-stream APIs. This article gives an overview about these features.