Top Java 9 Features You Must Know (2023) (original) (raw)

The Object-Oriented Programming, Java was designed by James Gosling and Java was maintained by Oracle Corporation (then Sun Corporation). This general-purpose programming language is class-based and is equipped with concurrent programming features. Additionally, it also has multi-threading features, is a static, safe, and strongly typed programming language. The file extension names of this programming language are .java or .class.

The release of Java 9 is a milestone for the Java ecosystem. Staying up to date with new releases is essential to keep up with the technology and learning more about the released versions will gear you closer to your Java, J2EE & SOA Certification. In this article, we will talk about Java 9 features. Java 9’s initial version was released on 21 Sep 2017 and delivers exceptional improvements and new enhancements to the previous versions of Java. Java 9 guarantees that your coding methods and habits are extensively modified and updated in a way that benefits facilitate you. The most prominent difference brought forth by Java 9 is the modularization of Java. This is one of the important developments brought in the Java programme after the onset of Lambdas in Java 8.

Java 9 is equipped with top-notch feature sets like Jshell (REPL), Jigsaw, collections, and some API changes under the hood. In this article, we will explore all Java 9 new features extensively. Before we explain the Java 9 features in detail let us take a quick glimpse at how Java 9 helped to overcome those anomalies present in the previous versions.

Confused about your next job?

In 4 simple steps you can find your personalised career roadmap in Software development for FREE

Expand in New Tab

Benefits of Java 9

Whether working with Java SE 9 or any other version, depending upon the business need, the addition of the most advanced features will bring forth advancements in the following:

Let us look at some of the features which are an absolute must-know about in Java 9:

Top Java 9 Features

1. Project Jigsaw in Java 9

The foremost purpose of this project is to acquaint everyone with the theory of modularity; support for formulating modules in Java 9 and then apply the same to the JDK i.e. modularize the JDK.

Here are some of the advantages of modularity :

There are several JEPs (JDK Enhancement Proposals), which are a part of this project, like:

2. Noteworthy API Updates: Stack Walking and Concurrency

One of the JEPs that Oracle has initiated is JEP 266: More Concurrency Updates. The update holds some importance and can solve interesting problems in parallelism and concurrency, which require updates in those libraries.

The changes and updates will be:

  1. Interfaces upholding the Reactive Streams publish-subscribe framework
  2. Extensive enhancements implemented to the CompletableFuture API like support for timeouts, delays, subclassing, and other methods
  3. Overall developments like small tweaks and javadoc spec rewording

Next, let us talk about the concurrency updates of Java 9 with CompletableFuture and java.util.concurrent.Flow. Flow is considered the Java implementation of the Reactive Streams API. Reactive Streams resolve the pain of back pressure. The build-up of data occurs when the rate of the incoming task is higher than the app’s ability to process them, which results in a buffer of unhandled data. As a part of the concurrency updates, CompletableFuture will be updated as well. This will fix the errors that came in after its introduction in Java 8 and will incorporate more reliable support for subclassing, support for timeouts and delays, and a few utility methods.

The second point that deserves mentioning is the Stack Walking API. Java 9 will improve the way you traverse stack traces. This marks an official Java way to concoct stack traces, rather than simply treating them as plain text.

3. Stream API Improvements

Java 9 has included two distinct methods to interact with Streams such as takeWhile and dropWhile methods. Also, it has incorporated two overloaded methods like iterate and nullable methods.

The two new methods dropWhile and takeWhile let you get portions of a stream on the basis of a predicate.

  1. For instance, on an ordered stream, takeWhile renders the “longest prefix” of elements that are taken from the stream and matches the given predicate, starting at the commencement of the stream. On the other hand, the remaining items that were not matched by takeWhile are returned by dropWhile.
  2. On an un-ordered stream, starting at the inception of the stream, a subset of the stream’s elements that match the given predicate (but not all) is returned by takeWhile. dropWhile returns the rest of the stream elements after dropping a subset of elements that match the given predicate.

InJava 9, Stream.ofNullable() method allows you to design a single-element stream that wraps a value if not null or is an empty stream otherwise. Stream.ofNullable() is quite similar to a null condition check when the context of stream API is taken into consideration.

4. Process API Updates

Before Java 5, the only method to generate a new process was to use the Runtime.getRuntime().exec() method. When Java 5 came into existence ProcessBuilder API was added which supported a more accurate way of spawning new processes. Now, Java 9 is incorporating a new way of acquiring information about current and spawned processes.

To learn information on any process, now you should apply java.lang.ProcessHandle.Info interface. Few of the information programmers can now obtain from Process instances are as follows:

To know about all the information of a newly spawned process, you can use process.toHandle() method to obtain ProcessHandle instance. Rest all of the things are as above

Additionally, you use ProcessHandle.allProcesses() to obtain a stream of ProcessHandle of all processes available in the system.

To obtain the list of all child processes, direct as well as n-level deep, you can make use of children() and descendants() method.

5. JShell – REPL Tool

A new command-line interactive tool, JShell is shipped with JDK 9 distribution (JEP 222) to assess statements, declarations, and expressions written in Java. JShell enables us to perform Java code snippets and get instantaneous results without having to build a solution or project.

Jshell is quite similar to what we have command window in Linux OS (The Linux command line is a text interface to your computer, which allows users to execute commands). The only difference is that JShell is Java-specific. It has loads of other abilities, other than fulfilling simple code snippets like

6. Platform and JVM Logging

JDK 9 has advanced logging in platform classes (JDK classes) and JVM components, with the help of a new logging API. It allows you to define a logging framework of your choice (for instance Log4J2) as a logging backend for logging messages from JDK classes. There are a few things you should learn about this API:

  1. The API is intended to be utilized by the classes in the JDK and not by application classes.
  2. For your application code, you will have to keep using other logging APIs.
  3. The API does not allow you to configure the logger programmatically.

The API comprises of the following:

JDK 9 also implemented a new command-line option, -Xlog , that provides you with one point of access to all messages logged from all classes of the JVM. The syntax to use the -Xlog option is as follows:

All the options are optional. If a previous part in -Xlog is missing, you have to use a colon for that part. Like, -Xlog::stderr symbolizes that all parts have defaulted, the output which is set as stderr.

7. HTTP/2 client in Java 9

Previously, developers often resorted to using third-party libraries, like Jersey, Apache HTTP, and others. Additionally, Java’s HTTP API predates the HTTP/1.1 specification, is synchronous, and difficult to manage. These limitations demanded a new API. The new HTTP client API implements the following:

8. Multi-Release JAR files

Now, JAR files can include classes that have the capability only to run on the Java version they were compiled for. To use the latest features of the Java platform on the newer versions, the library developers will have to release a more current version of their library. Shortly, there will be various versions of the library being supported by the developers, which can be a nightmare. To surmount this shortcoming, these Java 9 features of multi-release JAR files empower developers to design JAR files with various versions of class files for several Java versions.

9. Microbenchmarks

The Java Microbenchmark Harness (JMH) is advancing forward to the next step in its development and is teaming up with Java as an established benchmarking solution. Java Benchmarking Harness is used for designing, administering, and assessing nano/micro/milli/macro benchmarks.

There are a few factors to consider, like warm-up times and optimizations, which could possibly have a huge impact on results.

Java Microbenchmark Harness is the perfect option if you are after the most reliable results to help you obtain the correct decision after your benchmarks.

Conclusion

So, these were all about Java 9 and new Java 9 features. There are various useful features that got dropped from Java 9. However, most of those features got implemented in Java 10.

A more light-weighted and standardized JSON API is talked about by many java developers. Nevertheless, it didn’t get implemented due to funding issues.

Mentioned below here are a few other features of Java 9 as well

FAQs

Q.1: What are the main goals of Java 9?

Ans: The chief goals for Java 9 are to:

Q.2: What is Process API in Java 9?

Ans: In Java 9, Process API is capable of managing and controlling operating system processes which has been improved considerably. ProcessHandle Class now grants the process’s native process ID, accumulated CPU time, start time, command, arguments, user, parent process, and descendants.

Q.3: Which method is added to the map in Java9?

Ans: of() was added, which is a convenient way to generate instances of the Map interface. It has the ability up to 10 key-value pairs.

Q.4: What is the difference between Java 8 and Java 9?

Ans: Java 8 and Java 9 are two separate versions that come under the umbrella of Java programming language, where Java 9 supports scripting language along with object-oriented programming language. Java 8 is equipped with Java Time API and Java IO improvements, whereas Java 9 comes with Money and Currency API updates.

Additional Resources