Java 9 comes with a bunch of new features. Do you want to know what is new and what has been deprecated?
- [Instructor] Hi, welcome to Section Two, Reactivity in Java 9. In the previous section, we learned what reactive programming is, its benefits, the entities involved, and how we can use it in Java. So, what is this section about? In this section we will see how Java 9 supports reactivity in a native way with the new Flow API. Not only that, we will also learn what's new in Java 9. Also, we are going to build reactive programming in practice. In the second video, we are going to create a real sample using Flow API.
We'll see a real implementation of a publisher, subscription, and a subscriber. We'll finish the section talking about functional programming. It's a concept which is related to reactivity quite often. Why is that related? Wait till the third video. But just a hint, we are going to see lambdas, which is a concept from functional programming. We are going to see them during the first two videos as well. So that you know, a lambda is just a block of code that is going to be executed later on in timing. Be ready and recognize them when you see that in the next videos.
Let's kick off with the first video of this section, Java 9 features. By the end of the video, you will know what's new in Java 9. We'll also see samples of new APIs, so you are going to be ready to put that into practice. As I mentioned before, we are going to see new Java 9 features, as well as what has been deprecated. Not everything is going to be new, let's go. First things introduced in Java 9, private interface methods. Java 8 brought default methods to the table so that interfaces could be evolved.
This went well, but there was one unfortunate detail: reusing code between default methods was unpleasant. Abstracted methods either had to be default methods, we had to go public, or go into some helper class to keep them private. Let's take a look at the sample to understand this better. This is code from an interface in Java 8. As you can see, both default methods, multiply and divide, are using the method inputZero which returns a boolean telling us if the input is zero or not.
We'd like that method to be private, but because it is in an interface, we cannot do this in Java 8. So in this case, it has to hold default as well, so that means public. How has that changed in Java 9? Let's move on to the next slide. Well, in Java 9 we can simply have a private interface method. It's not going to be public anymore, see line 37? We defined it as private and there you go, easy. Next feature is Collection Factory Methods. Java 9 provides us static factory methods on the collection interfaces that will create compact and more firewall collection instances.
The API is kept minimal. First of all, they're immutable. This is quite important, see line 23? The first one. We are creating a list of one element with the string one, and then to that list we are adding a newer string, one. Because the list is immutable, that line is going to fail and throw an unsupported operation exception. Unlike it, these factory methods will be used to insulate fields with a collection of fixed elements where immutable abbreviation in small cases is undesired.
If you heard that Java 9 will support immutable collections, then beware that this is it. The immutability did not make it into the type system. Another thing, the factory methods reject newel values. Normally where as elements, as keys, or as values, they are forbidden. We can see how we can create instances of a map with a method, map.of or map.ofentries. Check out the lines 25 through 36. Pretty cool stuff. Another feature is Process API.
It lets you control and manage operating-system processes. So far, there has been a limited availability for controlling and managing operating-system processes with Java. The limitations of the current API often force developers to resort to native code, and usually that in the [inaudible]. Pretty horrible, uh? Now with Java 9 provides limited support for native operating-system processes. Check on the second image out. Just with one line you can get the current process Pid. Another feature is Stack Walking. Java 9 defines an efficient standard API for stack walking that allows easy filtering of and lazy access to the information in the stack traces.
You can see that in the sample. We have that method in line 30 to walk through the stack, filtered those with our package name, and then return in just 10 elements. Don't worry if you are not familiar with the syntax. Those are lambdas that we'll discuss in the third video. Then in the main method, line 27, we're just printing its element out. Now we're introducing HTTP/2 clients, native support in Java 9. The HTTP client API implements HTTP/2 and WebSocket.
This new API will replace the old HTTP URL connection APIs, which is as old as HTTP/1.1 itself. The new API will support all the new features of HTTP/2, but tries to espouse only those who are element for the user. It will provide an event-based system for event notifications. For example, when a header or body is received, when an error occurs, or when the server initiates a server push. How to use this in Java? Well, as easy as we can see in the images, we create the HTTP response passing a URI.
What's the difference between the two snippets of code? The first one is synchronous and the second one, asynchronous. Can you spot the difference? In the second one, we use response Async instead of response. For that we need to pass a parameter where it has to be executed when the response comes. As you can see, inside that block we have the same code as we have in the synchronous version. We have the status code, the body, and we print that out. In the synchronous sample, we want to do that when we had the response.
What more? Project Jigsaw's goal is to make Java modular and break the Java runtime environment, GRE, to interrupt portable components, one of the most high features for Java 9. This JDK enhancement proposal is the first out of four steps towards Jigsaw, and will not change the actual structure of the GRE and JDK. The purpose of this step is to reorganize the JDK source code into modules, enhance the build system to compile modules, and enforce module bundles on built time.
The motivation behind this is to make Java easily scalable to a small computing device. For example, for internet of things, improve security and performance, and make it easy for developers to construct and maintain libraries. Light-weight JSON API provides a lightweight API for consuming and generating JSON documents and data streams. Currently, there are alternatives available for handling JSON in Java. What's unique about this API is that it would be part of the language and could use the new capabilities of Java 8.
Multi-resolution images. It defines a multi-resolution image API so that images with a resolution variance can easily be manipulated and displayed. A multi-resolution image will behave in the same way as an ordinary image. Platform logging API and service summary. Were you ever frustrated by the fact that the JDK classes use their own logging infrastructure? It defines a minimal logging API with platform classes can use to log messages together with a service interface for consumers of those messages.
A library of replication can provide an implementation of this service in order to route platform log messages to the logging framework of your choice. If no implementation is provided, then a default implementation based upon the Java Uti login API will be used. The HTML 5 Javadoc summary enhances the Javadoc tool to generate HTML 5 markup. It provides an option to Javadoc to request either HTML 4 or HTML 5 output.
It's worth mentioning that the pages generated using HTML 5 markup should satisfy accessibility of requirements. The last feature we are going to talk about today is the Datagram Transport Layer Security, DTLS. It adds DTLS support, a secure way to communicate using datagram protocol, such as UDP. Okay, that was a pretty long list. These are some new features but, is everything we had in the past still there? Well, not everything.
We'll, let's see. Which APIs have been removed? The applet API, Java in the browser. Remember that thing? Corba is also deprecated, and explicit constructors for primitive wrappers. For example, as you used to do new integer five have been deprecated in favor of factory methods, such as bally off or parse. This is supposed to improve performance. And that's it for this video. Hopefully, you got an overview of what's new in Java 9, and how to use some of the new APIs.
In the next video, we're going to see how Java 9 supports reactive programming natively. Flow API, I will see you there.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- What is reactive programming?
- Java 9 reactive features
- Creating and observing sources with RxJava
- Unit testing
- Akka streams in a reactive environment
- Building a sample reactive application