Java 9

Java 9 was released for General Availability (GA) on September 21, 2017. There are many new features shipped with Java 9. Among them, modularity is the defining feature for Java 9.

So far, there has been a problem of modularity in Java, especially significant for large codebases. Every public class can be accessed by any other class in the classpath, leading to inadvertent usage of classes. In addition, the classpath presents potential problems, such as the inability to know whether or not there are duplicated JARs. To solve these problems, Java 9 provides the Java Platform Module System, which allows to create modular JAR files. This type of modules contains an additional module descriptor called module-info.java. The content of such files is quite simple: it declares dependencies to other modules using the keyword requires, and exports its own packages with the keyword exports. All non-exported packages are encapsulated in the module by default, for example:

module mymodule {
exports io.github.bonigarcia;

requires mydependency;
}

We can represent the relationship between these modules as follows:

Example of relationship between modules in Java 9

Other new capabilities of Java 9 are summarized in the following list:

  • The use modules allow to create a minimal runtime JDK optimized for the given application, instead of using a fully JDK installation. This can be achieve using the tool the jlink shipped with JDK 9.
  • Java 9 provides an interactive environment to execute Java code, directly from the shell. This type of utility is commonly known as Read-Eval-Print-Loop (REPL), which is called JShell in JDK 9.
  • Collection factory methods, Java 9 provides the capability of creating collections (for example, lists or sets) and populates them in a single line:
      Set<Integer> ints = Set.of(1, 2, 3);
List<String> strings = List.of("first", "second");
  • Stream API improvements: Streams was introduced in Java 8, and they allow to create declarative pipelines of transformations on collections. In Java 9, the methods dropWhile, takeWhile, and ofNullable are added to the Stream API.
  • Private interface methods: Java 8 provides default methods on interfaces. The limitation so far is that default methods in Java 8 must be public. Now, in Java 9, these default methods can be also private, helping to structure better their implementation.
  • HTTP/2: Java 9 supports out of the box, version 2 of HTTP and also WebSockets.
  • Multi release JARs: This feature allows to create alternative versions of classes, depending on the version of the JRE executing the JAR. To that aim, under the folder META-INF/versions/<java-version>, we can specify different versions of compiled classes, which will used only when the JRE version matches the version.
  • Improved Javadoc: Last but not least, Java 9 allows to create HTML5 compliant Javadoc with an integrated search capability.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.235.219