The top 9 improvements and features of Java 9, programmers can first understand the next

Source: Internet
Author: User
Tags stream api http 2

The top 9 improvements and features of Java 9, programmers may wish to understand the 2017-08-04 10:13 programming/Operating System/Client Java 9 is coming soon. Are you ready for new features that will come with the new version? In this paper, the author of the important ecosystem of Java changes, including the new module system, language and grammar changes, etc., to carry out a comprehensive introduction.

  

Java 9 as the release date approaches, the interest of programmers and developers in the new features is increasing. The release date for the new Java version is September 21, 2017. There's less than two months! There are a lot of changes in Java 9, and below I will list the 9 important improvements that have been added to the new Java 9.

1. New module system

There are several issues with writing large applications or maintaining libraries. As the code base grows, the chance to create complex code increases. It is difficult for each user to actually encapsulate the code class as part of the public API, and there is no clear dependency concept between the different parts of the system.

The jigsaw included in the new Java version is designed to address all of these issues. The module will consist of a common class and a new module declaration file. The module descriptor clearly defines what dependencies our modules need and which modules are used externally. All packages not mentioned in the EXPORTS clause are encapsulated in the module by default.

A simple module declaration that exports some of its packages:

  

You can access the Quick Start guide here if you want to learn how to build an application module or familiarize yourself with more examples of jigsaw syntax and projects.

Http://openjdk.java.net/projects/jigsaw/quick-start

2. Support http/2.0

The main difference between http/1.1 and HTTP/2 is how to build and transfer data between the client and the server. The http/1.1 relies on the request/response cycle. HTTP/2 allows the server to "push" data: It can send more data than the client requests. This allows it to prioritize and send data that is critical to loading the page first. Java 9 will fully support HTTP 2.0 and provide Java with a new HTTP client, which will override the httpurlconnection– for blocking mode only for each pair of requests/responses that have one thread, which increases the latency and load time of the Web page. HTTP clients also provide APIs to handle HTTP functions such as HTTP and server push.

Two HTTP interaction examples are shown below, and they come from a Java 9 document:

  

3. Improved Javadoc

Based on my experience working in the Java Development company Dreamix. Currently, if you want to find some class documents, you must search for them in Google. In Java 9, Javadoc has several improvements, one of which is to add a search box.

  

In Java 8

  

In Java 9

4.Stream improvements

The Stream API is one of the game-changing features in Java 8, and Java 9 has become even better. Now you can create the stream from Optional. The stream interface also adds four new methods: Iterate,dropwhile,takewhile,ofnullable.

Dropwhile discards the first item of a stream until the condition is met.

TakeWhile process the item until the condition is met.

Iterate allows you to use stream to write the appropriate substitution for the For loop. It requires the initial value of the stream, defines when the condition of the iteration is stopped, and the step function that generates the next element.

Ofnullable as a name suggests letting you create a stream from an object without checking for null. It returns a sequence containing a single element stream, or an empty stream if it is not empty.

5. Use the new factory method to initialize collections more easily

Currently, if you want to create a list of predefined values, you must make a lot of input:

  

In the future, the initialization of common collections will be easier with the newly added factory method. The static method in the interface makes it possible to enhance the list, collection, and mapping interfaces to create a collections method with up to 10 elements. The generated objects are immutable collections that are optimized for performance.

Adding items to these collections after creation will result in "Unsupportedoperationexception".

The code above looks more beautiful:

These are some of the added methods:

  

6. Private methods in the interface

Java 8 gives the default method in our interface. These methods have the body and the behavior to the interface, not only the empty signature. What would you do if you had two public methods that were almost identical? It is likely that you will attempt to move the generic code privately and call them from public methods. But in a similar situation, would you use two default methods in the interface instead of two public methods in the class?

In Java 9, you can use the exact same method and have a private method of common logic that will not be part of your API.

7. Improvements in language and grammar

It is now easier to write a try with a resource statement. Previously, all resources that must be closed after execution must be initialized in a try clause, as shown in the following example:

Starting with Java 9, we can use the final and valid final resources in a try clause:

  

Variable names from Java 9 cannot consist of a single underscore ("_"). You can write an underscore in the variable name in My_var, but a separate underscore will result in an error. The reason behind this is that the underscore will be retained for future use in the language.

We will be able to work with the anonymous inner class using the diamond operator (operator):

8. Enhanced Processing API

So far, the ability to manage and control operating system processes has been limited. In addition, the code that you write to perform such interactions depends on the operating system.

The new version expands the ability to interact with the operating system. New methods will be added to handle PID management, process name and state, child process management, and so on.

The sample code that retrieves the current process PID and runs on all operating systems is as follows:

9.Java REPL = Jshell

Last but not least Java9 will include the Read Evaluate Print Loop (REPL) tool in Project Kulla (http://openjdk.java.net/projects/kulla/). This command-line tool is called Jshell, and if you want to write a few lines of code yourself to test it, it will be the perfect tool.

You do not need to use the new class of the main method simply by executing a simple command.

  

What do we see in the new version?

There are several good features that are removed from the upcoming release. However, you can wait in Java 10.

A standardized and lightweight JSON API is favored by many Java developers. But because the funding problem cannot be seen in Java 9, it will not be cut off. "This Jep will be a useful addition to the platform, but in the plan it is not as important as other features Oracle grants, and may be reconsidered in JDK 10 or later," said Mark Reinhold, chief architect of the Java platform, in the JDK 9 message column. ”

  Shenzhen die-casting Factory http://www.dosin.cn/

The top 9 improvements and features of Java 9, programmers can first understand the next

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.