java9
Table of Contents
- JPMS - Java Platform Module System
- One of the main motivations here is to provide modular JVM, which can run on devices with a lot less available memory.
- The JVM could run with only those modules and APIs which are required by the application.
- Check out this link for a description of what these modules are. https://cr.openjdk.org/~mr/jigsaw/ea/module-summary.html
- For more in-depth example check OpenJDK. https://openjdk.org/projects/jigsaw/quick-start
- Publish-Subscribe Framework
- Java’s built-in support for
Observer interface
and theObservable class
was deprecated. - The class
java.util.concurrent.Flow
provides interfaces that support theReactive Streams publish-subscribe framework
.- These interfaces support interoperability across a number of asynchronous systems running on JVMs.
- We can use utility class SubmissionPublisher to create custom components.
- Java’s built-in support for
- Private methods in interfaces
- Interfaces in the upcoming JVM version can have private methods, which can be used to split lengthy default methods
interface InterfaceWithPrivateMethods { private static String staticPrivate() { return "static private"; } private String instancePrivate() { return "instance private"; } default void check() { String result = staticPrivate(); InterfaceWithPrivateMethods pvt = new InterfaceWithPrivateMethods() { // anonymous class }; result = pvt.instancePrivate(); } }
- Interfaces in the upcoming JVM version can have private methods, which can be used to split lengthy default methods
- Factory methods to create Unmodifiable collections
-
Immutable Set
Set<String> strKeySet = Set.of("key1", "key2", "key3");
- You can also convert an entire array into a Set with the same method.
-
- StreamAPI enhancements
-
Optional to Stream
List<String> filteredList = listOfOptionals.stream() .flatMap(Optional::stream) .collect(Collectors.toList());
- java.util.Optional.stream() gives us an easy way to you use the power of Streams on Optional elements
-
- Java try with resources enhancements
- Diamond operator enhancements
- Now we can use diamond operator in conjunction with anonymous inner classes
FooClass<Integer> fc = new FooClass<>(1) { // anonymous inner class }; FooClass<? extends Integer> fc0 = new FooClass<>(1) { // anonymous inner class }; FooClass<?> fc1 = new FooClass<>(1) { // anonymous inner class };
- Now we can use diamond operator in conjunction with anonymous inner classes
- Process API updates
- The best feature according to some opinions
- The process API has been improved for controlling and managing operating-system processes.
- The class java.lang.ProcessHandle contains most of the new functionalities
- Java Process API
- HTTP/2 client
- A long-awaited replacement of the old HttpURLConnection.
- The new API is located under the java.net.http package.
- It should support both HTTP/2 protocol and WebSocket handshake, with performance that should be comparable with the Apache HttpClient, Netty and Jetty.
HttpRequest request = HttpRequest.newBuilder() .uri(new URI("https://postman-echo.com/get")) .GET() .build(); HttpResponse<String> response = HttpClient.newHttpClient() .send(request, HttpResponse.BodyHandler.asString());
- JShell
- JShell is read–eval–print loop – REPL for short.
- Simply put, it’s an interactive tool to evaluate declarations, statements, and expressions of Java, together with an API. It is very convenient for testing small code snippets, which otherwise require creating a new class with the main method.
- The jshell executable itself can be found in <JAVA_HOME>/bin folder:
jdk-9\bin>jshell.exe | Welcome to JShell -- Version 9 | For an introduction type: /help intro jshell> "This is my long string. I want a part of it".substring(8,19); $5 ==> "my long string"
- JLink - Java Linker
- SafeVarargs annotations
- G1GC - Garbage First Garbage Collector