Java 9 is a major release of Java which introduced the concept of Java platform module system. In addition Java 9 includes enhancements to the Java core libraries, JVM tuning, Security, Deployment and JDK Tools.
Since Java 9 is a major release after Java 8, questions on the features added in Java 9 are frequently asked in Java interviews. This is usually to check if you are up-to-date with the latest changes in Java programming language.
Java 9 is a major enhancement release of the platform containing enhancements to the Java core libraries, JVM, security, tools etc. Following are some of the key features and enhancements introduced in Java 9.
1. Java Platform Module System
2. Factory Methods for Collections
3. Private methods in Interfaces
4. Improvements to Streams API
5. New tool JShell
6. New tool JLink
7. Support for HTTP/2
Java 9 introduced a new concept and component called 'Module' which adds a higher level of aggregation over packages. A module consists of a group of related packages and resources which are reusable. In addition, the module specifies the list of external modules that it is dependent on, and also list of its packages that it makes available to other modules.
Java 9 divides the JDK into several modules that support different configurations. The Java command 'java --list-modules' lists out all the modules in the JDK. Modularization in JDK enables to greatly reduce the runtime size of Java application. For example, if you have a java application for IoT devices and does not have the need for Java GUI classes, then you can create a specific runtime that does not include the GUI modules.
A module declaration begins with the keyword 'module' followed by the module name and curly braces {..} for the body. The module body can contain various module directives such as requires, exports, uses etc.
Following code snippet highlights the module declaration syntax.
module moduleA {
//module dependency
requires moduleB;
//packages made available to other modules
export packageA;
export packageB;}
A module is defined in a module declaration file, which is a file named 'module-info.java' in the project’s root folder. The module declaration gets compiled into module-info.class in the JAR’s root. This is called the module descriptor.
A module has three properties - name, dependencies and exports
A module declaration begins with the keyword 'module' followed by the module name and curly braces {..} for the body. The module body can contain various module directives such as requires, exports, uses etc.
Following code snippet highlights the module declaration syntax.
module moduleA {
//module dependency
requires moduleB;
//packages made available to other modules
export packageA;
export packageB;}
No, module names have to be unique. if multiple modules have the same name at compile time, a compilation error occurs. If multiple modules have the same name at runtime, an exception occurs.
Yes, a module and a package within that module have the same name.
Java 9 added static factory methods for collections that can be used to easily create small immutable collections with minimum lines of code.
Examples of these methods are List.of(), Set.of() and Map.of().
List.of('US','UK','CA')
Set.of('US','UK','CA')
Map.of('US','United States','UK','United Kingdom','CA',''Canada)
In Java 9 we can define private and private static methods. These methods can be used by other methods in the interface, hence it prevents code duplication and enables code reuseability. By defining them as private we prevent the sub-classes from accessing these methods.
Private methods must contain a body and they cannot be abstract.
Prior to Java 7 Interfaces could contain only 'public abstract' methods.
Since Java 8, in addition to 'public abstract' methods you could also have 'public static' methods and 'public default' methods.
Since Java 9, in addition to 'public abstract', 'public static' and 'public default' methods; you can have 'private' methods. Private methods can be called from static methods and default methods from within the interface. private methods in interfaces are not accessible to the implementation class.
public interface MyInterface {
//abstract method
public abstract void method1();
//default method
public void method2() {
...
}
//static method
public void method3() {
...
}
//private method
private void method4() {
...
}
}
Java 9 introduced the following new features to the Streams API
1. takeWhile(Predicate p): If the stream is ordered, then this method returns a stream consisting of the longest prefix of elements taken from this stream that match the given predicate. If the stream is unordered, then this method returns a stream consisting of a subset of elements taken from this stream that match the given predicate.
2. dropWhile(Predicate p): If the stream is ordered, then this method returns a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate. If this stream is unordered, then this method returns a stream consisting of the remaining elements of this stream after dropping a subset of elements that match the given predicate.
3. iterate():
4. ofNullable(): This method returns a sequential Stream containing a single element, if non-null, otherwise returns an empty Stream.
JShell is an interactive REPL (Read-Eval-Print-Loop) tool provided in Java 9 which is useful for learning the Java programming language and prototyping Java code. You can launch JShell from the console and directly type and execute Java code. The immediate feedback of JShell makes it a great tool to explore new APIs and try out language features.
To start JShell, enter the command 'jshell' on the command line.
% jshell
| Welcome to JShell -- Version 9
| For an introduction type: /help intro
jshell>
To exit JShell enter the command '/exit' on the command line.
jshell> /exit
| Goodbye
JLink is Java’s new command line tool through which you can create you own customized JRE by linking sets of modules (and their transitive dependencies) to create a run-time image.
JLink will allow you to both simplify and reduce the size of deployment.
Java 9 introduced new class that handles http/2 connections.
These classes are
1. HttpClient - which handles the creation and send of requests.
2. HttpRequest which is used to construct a request to be sent via the HttpClient.
3. HttpResponse which holds the response from the request that has been sent.