Now Java has a diversified development trend, both JS and C++ and C# shadow, do not learn that is not a drop.

So, spend five minutes looking at the seven new features of Java9-Java11, along with some code samples.

Java11 has been released, but many companies are still using Java 8. Here’s a quick look at the new syntax and apis introduced in Java 9-11.

Local variable type inference

Java 10 has introduced a new keyword, var, which replaces the type information when declaring local variables. Local refers to a variable declaration within a method. Before Java 10, you had to declare a String like this.

In Java10, you can use var instead of String. The expression looks like this:

Variables declared with var are still statically typed. Incompatible types cannot be reassigned to such variables. This code fragment cannot compile:

Var is also disallowed when the compiler cannot infer the correct variable type. All of the following code examples result in compiler errors:

Local variable type inference can be generic. In the next example, the Map

> types can be reduced to a single var keyword to avoid a lot of boilerplate code:
,>

Since Java 11, lambda arguments are also allowed to use the var keyword:

HTTP Client

Java 9 began to introduce the HttpClient API to handle HTTP requests. Starting with Java 11, the API officially entered the standard library package (JavA.net). Let’s explore what we can do with this API. The new HttpClient can be used synchronously or asynchronously. Synchronization requests block the current thread. BodyHandlers define the expected type of the response body (for example, a string, byte array, or file) :

You can also use asynchrony to perform the same request. Calling sendAsync does not block the current thread, but instead returns a CompletableFuture to perform an asynchronous operation.

We can omit.get () because it is the default request method.

The next example uses POST to send data to a given URL. Similar to BodyHandler, you use BodyPublishers to define the types of data you send as the body of a request, such as strings, byte arrays, files, or input streams:

The final example demonstrates how to perform authorization with basic-auth:

Collections

Collections such as lists, sets, and maps have been extended in new ways. List.of creates a new immutable List from the given argument. List.copyOf creates an immutable copyOf a List.

Because the list is already immutable, there is no need to actually create a copy of the list instance, so the list and the copy are the same instance. However, if you copy a mutable list, the copy does generate a new instance, so make sure there are no side effects when changing the original list:

When creating an immutable map, you don’t have to create a map entry yourself, but instead pass keys and values as arguments:

Immutable collections in Java 11 still use the old interface in the Collection API. However, if you try to modify the immutable collection, will throw out the Java. Lang. UnsupportedOperationException. The good news is that if you try to change an immutable set, Intellij IDEA will give you a warning

Streams

Streams was introduced in Java 8, and Java 9 added three new methods. Single parameter constructor:

Add takeWhile and dropWhile methods to free elements from stream:

If you are not familiar with Stream, you can refer to this article [1].

Optionals

Optionals provides some very convenient features, such as you can now simply convert an Optional to a Stream, or provide another Optional for empty Optinal as a backup:

Strings

Java11 adds some helper methods to strings to trim or check Spaces:

InputStreams

InputStream adds a transferTo method that can be used to transfer data directly to an OutputStream:

Other VM features introduced many new features from Java 8 to Java 11. Here is a list of these features:

· Flow API for reactive programming

Java Module System

· Application Class Data Sharing

, Dynamic Class – the File Constants

Java, a REPL (JShell)

, the Flight Recorder

Unicode, 10

· G1: Full Parallel Garbage Collector

· ZGC: Scalable Low-Latency Garbage Collector

· Epsilon: No-Op Garbage Collector

· Deprecate the Nashorn JavaScript Engine

Translator’s note: Application class-Data Sharing (CDS),ZGC and Flight Recorder are more attractive for translators. For more information on ZGC, see the previous article on high Availability Architecture on ZGC.

Original address:

Winterbe.com/posts/2018/…

This article is written by Benjamin, translated by Fangyuan. Reprinted from the public account high availability architecture



Netease Cloud letter, the instant messaging and audio and video technology experts around you, understand us, please poke Netease cloud letter official website

For more industry insights and technical tips, please follow the netease Yunxin blog

More exciting content, pay attention to netease Cloud letter Zhihu organization number oh.