java9

Table of Contents
  1. JPMS - Java Platform Module System
    1. One of the main motivations here is to provide modular JVM, which can run on devices with a lot less available memory.
    2. The JVM could run with only those modules and APIs which are required by the application.
    3. Check out this link for a description of what these modules are. https://cr.openjdk.org/~mr/jigsaw/ea/module-summary.html
    4. For more in-depth example check OpenJDK. https://openjdk.org/projects/jigsaw/quick-start
  2. Publish-Subscribe Framework
    1. Java’s built-in support for Observer interface and the Observable class was deprecated.
    2. The class java.util.concurrent.Flow provides interfaces that support the Reactive Streams publish-subscribe framework.
      1. These interfaces support interoperability across a number of asynchronous systems running on JVMs.
    3. We can use utility class SubmissionPublisher to create custom components.
  3. Private methods in interfaces
    1. 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();
          }
      
      }
      
  4. Factory methods to create Unmodifiable collections
    1. Immutable Set

      Set<String> strKeySet = Set.of("key1", "key2", "key3");
      
      1. You can also convert an entire array into a Set with the same method.
  5. StreamAPI enhancements
    1. Optional to Stream

      List<String> filteredList = listOfOptionals.stream()
          .flatMap(Optional::stream)
          .collect(Collectors.toList());
      
      1. java.util.Optional.stream() gives us an easy way to you use the power of Streams on Optional elements
  6. Java try with resources enhancements
  7. Diamond operator enhancements
    1. 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
      };
      
  8. Process API updates
    1. The best feature according to some opinions
    2. The process API has been improved for controlling and managing operating-system processes.
    3. The class java.lang.ProcessHandle contains most of the new functionalities
    4. Java Process API
  9. HTTP/2 client
    1. A long-awaited replacement of the old HttpURLConnection.
    2. The new API is located under the java.net.http package.
    3. 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());
      
  10. JShell
    1. JShell is read–eval–print loop – REPL for short.
    2. 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.
    3. 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"
      
  11. JLink - Java Linker
  12. SafeVarargs annotations
  13. G1GC - Garbage First Garbage Collector

References

  1. https://www.baeldung.com/new-java-9

Links to this note