Released on September 21, 2017.
- JShell is a new tool that comes with Java 9. It is an interactive tool that allows you to write and execute Java code.
- It allows you to write and execute Java code using a command line interface.
// JShell
jshell> int a = 10;
a ==> 10
jshell> int b = 20;
b ==> 20
jshell> int c = a + b;
c ==> 30
jshell> System.out.println(c);
jshell> /exit- Java Platform Module System (JPMS) is a new feature added to Java 9. It is a new way to organize Java code into modules.
- Similar to Java package, modules introduce another level of Java code organization. Each module is a collection of packages. A module is declared by adding a module-info.java file to the root of the module.
module abc.xyz {
}module abc.xyz {
exports com.demo.common;
}- Java 9 introduced some new methods in the Collection API. Here some of the notable changes include:
The List.of, Set.of, and Map.of factory methods allow creating immutable collections with a small number of elements.
List<String> players = List.of("Messi", "Ronaldo", "Neymar");
Set<String> cars = Set.of("BMW", "Audi", "Mercedes");
Map<String, Integer> players = Map.of("Messi", 10, "Ronaldo", 7, "Neymar", 10);The Set.ofEntries and Map.ofEntries factory methods allow creating immutable maps and sets with a small number of entries.
Map<String, Integer> players = Map.ofEntries(
entry("Messi", 10),
entry("Ronaldo", 7),
entry("Neymar", 10)
);The Iterable.forEach method is now a default method, allowing it to be used with older classes that implement the Iterable interface.
List.of("Messi", "Ronaldo", "Neymar").forEach(System.out::println);The Arrays.compare method allows comparing two arrays lexicographically.
int[] a = {1, 2, 3};
int[] b = {4, 5, 6};
int result = Arrays.compare(a, b);The Arrays.mismatch method allows finding the first index at which two arrays differ.
int[] intArray1 = {1, 2, 3, 4, 5};
int[] intArray2 = {1, 2, 3, 8, 9};
int result = Arrays.mismatch(intArray1, intArray2);
System.out.println(result); // 3The Arrays.equals method has been overloaded to allow comparing two arrays for equality.
String[] playerList1 = {"Messi", "Ronaldo", "Neymar"};
String[] playerList2 = {"Messi", "Ronaldo", "Neymar"};
boolean result = Arrays.equals(player1, player2);
System.out.println(result); // true- Java 9 introduced some new methods in the Stream API. Here some of the notable changes include:
The takeWhile method allows taking elements from a stream while a given predicate is true.
List.of("Messi", "Ronaldo", "Neymar").stream()
.takeWhile(player -> player.length() > 5) // or dropWhile : droping elements from a stream while a given predicate is true.
.forEach(System.out::println);The Stream.ofNullable method allows creating a stream from a single element that may be null.
Stream.ofNullable("nanodev").forEach(System.out::println);The Stream.iterate method allows creating an infinite stream by repeatedly applying a function to the previous element.
Stream.iterate(1, n -> n + 1)
.limit(10)
.forEach(System.out::println);These streams now have methods for calculating the sum, average, min, and max of elements in the stream.
IntStream.of(1, 2, 3, 4, 5)
.sum(); // 15
IntStream.iterate(1, n -> n + 1)
.limit(10)
.average(); // 5.5
IntStream.of(2, 4, 6, 8, 10, 15, 20)
.dropWhile(i -> i%2 == 0)
.forEach(System.out::println); - Java 9 introduced a new HTTP client API, which is built on top of the new HTTP/2 protocol.
- This new API provides a more efficient and powerful way to send and receive HTTP requests and responses. It supports full-duplex communication, allowing multiple requests and responses to be sent and received simultaneously over a single connection.
Java 9 enhanced the JAR file format which can contain multiple Java-release-specific versions of our class files to coexist in a single archive. Using this feature, we can upgrade our application/library to new version of Java without forcing the users to upgrade to the same Java version.
- To create a multi-release JAR file, we need to declare the
Multi-Releaseattribute in the JAR manifest file META-INF/MANIFEST.MF.
Multi-Release: trueSuppose later in the future Java 10 release, it is decided to upgrade class A and class B to a new version. We can create a new directory named META-INF/versions/10 in the JAR file and place the new version of class A in it.
jar root
- classA
- classB
- META-INF
- versions
- 10
- classA
- 10
- classBResouces:
