Java 9 – an overview of the new features
Java 9 comes to your doorstep with major changes for all of us, whether we ordered it or not. Modularity is the big theme of the Java 9 release, and it requires rethinking how we structure, build, and run Java applications. This is great, because who doesn’t like more reliable and secure applications, meanwhile killing the dreaded classpath? Additionally, Java 9 has several other smaller, but useful, features, including support for HTTP 2 and many more.
Schedule for Java 9
2016/05/26 Feature Complete 2016/08/11 All Tests Run 2016/09/01 Rampdown Start 2016/10/20 Zero Bug Bounce 2016/12/01 Rampdown Phase 2 2017/01/26 Final Release Candidate 2017/03/23 General Availability
Features of Java 9 –
1Java + REPL = jshell
The next release of Java – JAVA 9 will feature a new command line tool called jshell that will add native support and popularize a Java way to REPL (Read-Eval-Print-Loop).
Provide an interactive tool to evaluate declarations, statements, and expressions of the Java programming language, together with an API so that other applications can leverage this functionality.
⋊> ~ jshell
| Welcome to JShell -- Version 9-ea
| For an introduction type: /help intro
jshell> 3 + 4 * 7
$1 ==> 31
jshell> int add(int a, int b) {
...> return a+b;
...> }
| created method add(int,int)
jshell> add(25, $1)
$3 ==> 56
2HTTP 2.0 is the future
Define a new HTTP client API that implements HTTP/2 and WebSocket, and can replace the legacy HttpURLConnection
API.
The HTTP API functions asynchronously & synchronously. In asynchronous mode, work is done in threads (ExecutorService).
The existing HttpURLConnection
API and its implementation have numerous problems:
- The base
URLConnection
API was designed with multiple protocols in mind, nearly all of which are now defunct (ftp
,gopher
, etc.). - The API predates HTTP/1.1 and is too abstract.
- It is hard to use, with many undocumented behaviors.
- It works in blocking mode only (i.e., one thread per request/response).
- It is very hard to maintain.
public static void main(String[] args) throws Exception {
HttpClient.getDefault()
.request(URI.create("http://www.tutorialsimplified.com"))
.GET()
.responseAsync() // CompletableFuture :D
.thenAccept(httpResponse ->
out.println(httpResponse.body(HttpResponse.asString()))
);
Thread.sleep(999); // Give worker thread some time.
}
3Process API Updates
Improve the API for controlling and managing operating-system processes. The limitations of the current API often force developers to resort to native code.
// Get PIDs of own or started processes
out.println("Your pid is " + ProcessHandle.current().getPid());
Process p = Runtime.getRuntime().exec("sleep 1h");
ProcessHandle h = ProcessHandle.of(p.getPid()) // Optional
.orElseThrow(IllegalStateException::new);
// Do things on exiting process // CompletableFuture
h.onExit().thenRun( ()-> out.println("Sleeper exited") );
// Get info on process
out.printf("[%d] %s - %s\n", h.getPid(),
h.info().user().orElse("unknown"),
h.info().commandLine().orElse("none"));
// Kill a process
h.destroy();
4The Modular JDK
Use the Java Platform Module System, specified by JSR 376 and implemented byJEP 261, to modularize the JDK.
Divide the JDK into a set of modules that can be combined at compile time, build time, or run time into a variety of configurations including, but not limited to:
- Configurations corresponding to the full Java SE Platform, the full JRE, and the full JDK;
- Configurations roughly equivalent in content to each of the Compact Profiles defined in Java SE 8; and
- Custom configurations which contain only a specified set of modules and the modules transitively required by those modules.
5Create PKCS12 Keystores by Default
Improve security. PKCS12 offers stronger cryptographic algorithms than JKS.
Maintain forward and backward compatibility. Applications that access JKS and PKCS12 keystores must continue to function across JDK releases.