Java 11 is officially released. These new features teach you to write more awesome code

Today, let's take a look at what Java 11 is, what's special about it, and whether to upgrade to Java 11.

What's special about Java 11

On the Oracle official website, enter the download page. The first version of JDK available for download has been changed to Java se 11 (LTS). Lts here means long term support.

The biggest difference between Java 11 released this time and Java 9 released in September 2017 and Java 10 released in March 2018 is that in terms of long-term support, Oracle said it would provide strong support for Java 11, which will last until September 2026. This is the first long-term version supported after Java 8.

Why is it a long-term version? See the official support roadmap below.

The above figure is a support roadmap for various versions of JDK published by Oracle. The official release time and support plan of Java 6 - Java 12 are listed on the way.

It can be seen that before Java 11, Java 9 and Java 10 did not provide long-term support, while the last version that provided long-term support was Java 8, which will be supported until March 2025.

Long term support means that Oracle will provide long-term patch, security and other extended support.

The next long-term supported version will be Java 17, which will be released in 2021.

Now most of them are using java 8, Java 9 and Java 10. At present, few people are using java 8, Java 9 and Java 10. At least I don't find any companies applying in the production environment, that is, looking for death.

Now Java 11 has long-term support and includes all the functions of 9 and 10. 9 and 10 will naturally live to the end..

So let's look at some important new features from Java 9 - 11?

New features

1. Local variable type inference

What is local variable type inference?

As you can see, local variable type inference is that the type on the left is directly defined by VAR without writing a specific type. The compiler can automatically infer the type according to the expression on the right, such as string above.

Is equal to:

2. String reinforcement

Java 11 adds a series of string processing methods, as shown below.

3. Set reinforcement

Since Java 9, the of and copyof methods have been added to sets (list / set / map) in JDK. Both of them are used to create immutable sets. Let's see their use and differences.

Example 1:

Example 2:

The code of examples 1 and 2 is similar. Why is one true and the other false?

Take a look at their source code:

It can be seen that the copyof method will first judge whether the source collection is of abstractimmutablelist type. If so, it will return directly. If not, it will call of to create a new collection.

Example 2 because the collection created with new does not belong to the subclass of immutable abstractimmutablelist class, the copyof method creates a new instance, so it is false

Note: the sets created with of and copyof are immutable sets, and cannot be added, deleted, replaced or sorted. Otherwise, Java. Com will be reported Lang.unsupported operationexception.

The above demonstrates the of and copyof methods of list. Both set and map interfaces have them.

4. Stream enhancement

Stream is a new feature in Java 8. Java 9 began to add the following four new methods to stream.

From the beginning, it ends when n < 3.

This is contrary to the above. Once n < 3 is not tenable, the calculation starts.

3) Iterate overload

This new overload of the iterate method allows you to provide a predicate to specify when to end the iteration.

If you are not familiar with stream in JDK 8, you can see this series of tutorials shared before.

5. Optional enhancements

Opthonal has also added several cool methods. Now it is easy to convert an option into a stream, or give it an alternative when it is empty.

6. InputStream enhancement

Finally, InputStream has a very useful method: transferto, which can be used to directly transfer data to OutputStream. This is a very common usage when processing the original data stream, as shown in the following example.

7、HTTP Client API

This is an incubated HTTP client API for handling HTTP requests introduced by Java 9. The API supports synchronization and asynchrony, and is officially available in Java 11. You can click "target =" in nofollow noopener noreferrer_ Blank "> find this API in the java.net package.

Let's take a look at the usage of HTTP client:

above. Get() can be omitted. The default request method is get!

For more usage examples, you can see this API and have a chance to demonstrate it later.

Now that Java comes with this HTTP client API, do we still need to use Apache's httpclient toolkit in the future?

8. Simplify the complexity, compile and run the source code with one command

Look at the code below.

In our understanding, to run a java source code, we must compile it first, then run it, and execute the action in two steps. In the future Java 11 version, it will be done directly through a Java command, as shown below.

More new features

In terms of new features, the newly released Java 11 provides 17 JEPs (JDK enhancement proposal)

The figure above shows all the new features included in Java 11 released by Oracle, including several key new features:

ZGC: scalable low latency garbage collector

ZGC is a garbage collector that claims to ensure that the pause time of each GC does not exceed 10ms, and the throughput decreases by no more than 15% compared with the current default garbage collection G1.

Epsilon: a garbage collector that does nothing

Java 11 also adds a special garbage collector epsilon, which is called "no OP" collector, and will handle memory allocation without implementing any actual memory recycling mechanism. In other words, this is a garbage collector that does not do garbage collection. This garbage collector doesn't seem to be useful. It can be used for performance testing, memory stress testing, etc. epsilon GC can be used as a control group to measure the performance of other garbage collectors. Martijn said that epsilon GC can at least help understand the interface of GC and help to achieve a more modular JVM.

Enhanced var usage

Java 10 adds the feature of local variable type inference. You can use VaR to define local variables. Although this feature has been criticized by many people, it does not affect java to continue to enhance its usage. In Java 11, VAR can be used as a local variable declaration of lambda expressions.

Remove Java EE and CORBA modules

As early as the release of Java se 9, Java said that it would remove Java EE and CORBA modules in future versions, and such a move was finally implemented in Java 11. Finally, Java EE and CORBA modules are removed.

HTTP client further upgrade

The HTTP client API has been standardized in JDK 9, and then updated in JDK 10 through JEP 110. In the update list of Java 11 this time, JEP 321 is used for further upgrading. The API provides non blocking request and response semantics through completeablefutures, which can be used together to trigger corresponding actions. JDK 11 completely rewrites this function. It is now easier to track the data flow between the user layer request publisher and response publisher and the underlying socket, which reduces complexity and maximizes the possibility of reuse between http / 1 and HTTP / 2.

Do you want to upgrade

In August 2017, the JCP Executive Committee proposed to change the release frequency of Java to every six months.

The JDK version most people use is java 8 or below, and even JDK 1.6 is used in the production environment of some companies.

So, for companies and developers, do they want to upgrade and learn java 11 in the production and development environment?

For enterprises

For enterprises, it is still necessary to upgrade the JDK version in the production environment to Java 11. There are two main reasons:

1. Oracle will provide long-term support for Java 11, and enterprises can rest assured to use this version. And the next long-term supported version will be released in three years, which is a long time ago.

2. Java 11 does provide some good features, especially ZGC, which is an epoch-making garbage collector. The advantages will not be repeated. With ZGC, the performance bottleneck of the JVM can be broken through.

For developers

In terms of coding, Java 11 has not changed as much as Java 8. After all, Java 8 provides the ability of functional programming, which is also an important reason for many developers to learn java 8.

However, Java 11 is not completely upgraded. At least in the new version, Java developers can finally get rid of the old httpurlconnection. The new HTTP API provides support for industry-leading standards such as httop / 2, and provides a concise and friendly API interface.

Therefore, to sum up, it is necessary for both enterprises and developers to upgrade Java 11, which is at least much more necessary than Java 9 and Java 10. As for how necessary is this, the author gives a simple explanation:

Many people are still using java 8 or 7, but 8 will end free updates in early 2019. Now 11 is a long-term support version, which is a good time to learn and start 11. I hope this article can inspire you.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>