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
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!!
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}
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();
}
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.
The following classes have been added.
ProcessHandle currentProcess = ProcessHandle.current();
System.out.println("Current Process Id: = " + currentProcess.getPid());
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());
}
}
CompletableFuture API,
Functions such as have been added.
Executor exe = CompletableFuture.delayedExecutor(50L, TimeUnit.SECONDS);
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.
I didn't know the details, so I omitted it.
Various additions have been made to the Optional class.
<T>
or(Supplier<? extends Optional<? extends T>> supplier)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)
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
Is it going to be abolished? (forRemoval) When will it be abolished? (since) It seems that information such as is now given.
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.
Other
And so on.
Recommended Posts