Java 9 new features and sample code

This is a reprint of Blog article.

With the Java 9 release approaching July 2017, I will introduce what you can do with Java 9 with sample code.

All information is before release, so Please note that it may differ in the actual Java 9 release.

Java10 release is approaching, I will post a link. Java 10 new feature summary

table of contents

  1. JShell
  2. Factory methods for immutable List / Set / Map
  3. Private method in Interface
  4. Modular system
  5. Process API improvements
  6. Improvement of try-with-resource
  7. CompletableFuture API improvements
  8. Reactive stream
  9. Diamond operator in anonymous class
  10. Optional class improvements
  11. Stream API improvements
  12. Enhanced @Deprecated annotation
  13. HTTP2 client
  14. Multi-Resolution Image API
  15. Others

JShell It is a REPL environment. Run Java from the command line, You can now check the API and perform simple tests.

C:\>jshell
|  Welcome to JShell -- Version 9-ea
|  For an introduction type: /help intro

jshell> String helloWorld = "Hello World"
helloWorld ==> "Hello World"

jshell> System.out.println(helloWorld + "!!" )
Hello World!!

Factory methods for immutable List / Set / Map

As the title says. You can now create List / Set / Map like Guava (Google's common processing library).

jshell> List emptyImmutableList = List.of();
emptyImmutableList ==> []
jshell> Map nonemptyImmutableMap = Map.of(1, "one", 2, "two", 3, "three")
nonemptyImmutableMap ==> {2=two, 3=three, 1=one}

Private method in Interface

In Java8, you can now define default and static methods for Interface. Java9 will also allow you to define private methods.

public interface Card{
  private Long createCardID();
  private static void displayCardDetails();
}

Modular system

It was called the Jigsaw project. It is now possible to define public API and private API in module-info.java.

You can find the sample code in the article here.

Process API improvements

The following classes have been added.

 ProcessHandle currentProcess = ProcessHandle.current();
 System.out.println("Current Process Id: = " + currentProcess.getPid());

Improvement of try-with-resource

The try-with-resource statement up to Java 8 It was mandatory to declare the class in the try clause, This is no longer needed.

void testARM_Java9() throws IOException{
 BufferedReader reader1 = new BufferedReader(new FileReader("journaldev.txt"));
 try (reader1) {
   System.out.println(reader1.readLine());
 }
}

Completable Future API improvements

CompletableFuture API,

Functions such as have been added.

Executor exe = CompletableFuture.delayedExecutor(50L, TimeUnit.SECONDS);

Reactive stream

To promote asynchronous processing / parallel processing / scalable application development The Publish / Subscribe Framework has been added. It is a mechanism in Akka etc.

Sample code can be found at here.

Diamond operator in anonymous class

I didn't know the details, so I omitted it.

Optional class improvements

Various additions have been made to the Optional class.

By passing Optional :: stream to Stream # flatMap It seems that it can be converted to Stream of the wrapped class.

Stream<Optional> emp = getEmployee(id)
Stream empStream = emp.flatMap(Optional::stream)

Stream API improvements

A takeWhile method and a dropWhile method that can be written in the same way as Scala have been added.

jshell> Stream.of(1,2,3,4,5,6,7,8,9,10).takeWhile(i -> i < 5 )
                 .forEach(System.out::println);
1
2
3
4

Enhanced @Deprecated annotation

Is it going to be abolished? (forRemoval) When will it be abolished? (since) It seems that information such as is now given.

HTTP2 client

It seems that this has been downgraded to the incubation function ... Reference

The HTTP / 2 Client is included in Java 9, but it will not be accessible by default. The functionality is packaged in a prefix module called jdk.incubator. Developers accessing it must explicitly use the --add-mod flag. However, if you choose to do this, you will need to consider that the incubation feature is not part of the standard API and is therefore always modified.

jshell> import java.net.http.*

jshell> import static java.net.http.HttpRequest.*

jshell> import static java.net.http.HttpResponse.*

jshell> URI uri = new URI("http://google.com")
uri ==> http://google.com

jshell> HttpResponse response = HttpRequest.create(uri).body(noBody()).GET().response()
response ==> java.net.http.HttpResponseImpl@79efed2d

jshell> System.out.println("Response was " + response.body(asString()))

Multi-Resolution Image API It seems that the MultiResolutionImage interface has been added to the java.awt.image package.

See Official doc for details.

Others

Other

And so on.

reference

Java 9 Features with Examples

Recommended Posts

Java 9 new features and sample code
Java sample code 02
Java sample code 03
Java sample code 04
java1.8 new features
Java sample code 01
Java version 8 and later features
Digital signature sample code (JAVA)
New features from Java7 to Java8
Java 15 implementation and VS Code preferences
Java features
Java features
Sample code to parse date and time with Java SimpleDateFormat
BloomFilter description and implementation sample (JAVA)
Java 14 new features that could be used to write code
Sample code using Minio from Java
Link Java and C ++ code with SWIG
Sample code collection for Azure Java development
Page number logic and reference code (java)
Script Java code
Sample code to serialize and deserialize Java Enum enums and JSON in Jackson
[Java] Generics sample
Selenium sample (Java)
About Java features
Java and JavaScript
XXE and Java
Java character code
[Java] Explanation of Strategy pattern (with sample code)
I touched on the new features of Java 15
Predicted Features of Java
Java 12 new feature summary
Sample code to convert List to List <String> in Java Stream
Getters and setters (Java)
[Java] Thread and Runnable
Sample (Java) for OAuth 2.0 authentication and access token acquisition
Java 13 new feature summary
Java true and false
[Java] String comparison and && and ||
[Java] Holiday judgment sample
Install java and maven using brew on new mac
Java --Serialization and Deserialization
[Java] Arguments and parameters
timedatectl and Java TimeZone
Sample code for log output by Java + SLF4J + Logback
[Java] Branch and repeat
[Java] logback slf4j sample
What's new in Java 8
Java 10 new feature summary
[Java] Variables and types
[Java] Three features of Java
java (classes and instances)
Java 14 new feature summary
What's new in Java 9,10,11
[Java] Overload and override
Java code sample to acquire and display DBLINK source and destination data in Oracle Database using DBLINK
Catch up on new features from Java 7 to Java 9 at once
Java source sample (Oracle Database + java) to SELECT and display CLOBs
Understand the Singleton pattern by comparing Java and JavaScript code
Correct the character code in Java and read from the URL
Eclipse installation and code completion enhancements (Mac for Java development)
Understand the Iterator pattern by comparing Java and JavaScript code