Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

After reviewing some of the features of Java 9 in the last article, we’ll take a look at what Java 10 brings. It is necessary to summarize the features of Java 8 through Java 17 because the Java community is paying more attention to Java 17 than ever before. Without further ado, let’s move on to Java 10.

Java 10

Starting with Java 10, the Java iteration cycle was shortened to six months, with a release every six months.

Local variable type inference

Initializing a Map in Java 6 requires us to declare:

 Map<String, String> map = new HashMap<String,String>();
Copy the code

In fact, the parameters of a generic method can be derived from context, so in Java 7 it is simplified to:

 Map<String, String> map = new HashMap<>();
Copy the code

With Java 10 taking type inference to the next level, let’s look at an example:

         var map = Map.of("hello","world");
         String var = map.get("hello");
Copy the code

At first glance, it looks like Javascript. In fact, it’s Java. The compiler deduces the initialization type from the type of the initializer on the right, which drastically reduces some boilerplate code. Note, however, that this feature only applies to initializing local variables; it cannot be used in scenarios of member variables, method parameters, return types, and so on.

Another thing to note is that var is not a keyword in Java, which ensures backward compatibility in Java. In addition, using VAR has no runtime overhead and does not make Java a dynamic language. The type of the var token variable is still inferred at compile time.

Var should not be abused

Although this is “fun”, VAR should not be abused.

The type of a variable needs to be DEBUG:

 var data = someObject.getData();
Copy the code

Try not to use it in streams:

Var names= apples.stream().map(Apple::getName).collect(Collectors. ToList ());Copy the code

Therefore, the necessary readability should be guaranteed when using VAR.

In addition, in the case of polymorphism, an important Java feature, VAR is not perfect. So if Fruit has both Apple and Orange implementations.

 var x = new Apple();
Copy the code

If we reassign x to new Orange(), we get an error because the type of x is fixed after compilation. So just like generics, var comes into play during compilation. You must ensure that the type of var is fixed.

So what happens when var is combined with the generic diamond symbol <>?

The following empList is of type ArrayList:

 var empList = new ArrayList<>();
Copy the code

If we want to make it clear that the collection is filled with Apples, we must explicitly declare on the right:

 var apples = new ArrayList<Apple>();
Copy the code

Immutable set

In fact, immutable collections have been somewhat enhanced in Java 9, and further enhanced in Java 10. Why are immutable sets so important?

  • Immutability is one of the cornerstones of functional programming, so strengthening immutable sets helps the development of functional programming in Java.
  • Security, because collections are immutable, there are no race conditions, and natural thread safety, which has advantages both in coding and in memory usage, is a feature that stands out in Scala and Kotlin programming languages.

Several new apis have been introduced in Java 10.

A copy of the collection

Copy a set as immutable:

  List<Apple> copyList = List.copyOf(apples);
Copy the code

Any attempt to modify such collection will result in Java. Lang. UnsupportedOperationException anomalies.

Stream is reduced to an immutable set

The Stream API’s inductive operation collect(Collector Collector), which used to generalize streams into mutable collections, now has immutable collections. Here’s an example:

 List<String> names= apples.stream()
     .map(Apple::getName)
     .collect(Collectors.toUnmodifiableList());
Copy the code

Optional.orElseThrow()

Optional<String> optional = Optional.ofNullable(nullableVal); // NoSuchElementException String Nullable = option.get ();Copy the code

Optional A NoSuchElementException will be raised if get is null. Semantically get should definitely get something, but in fact it’s an exception, which is too ambiguous. So an orElseThrow() method was added to enhance the semantics.

Other enhanced features

Java 10 also has significant performance enhancements, including support for G1 parallel garbage collection. In addition, just-in-time compilation (JIT) technology was introduced, which can speed up the running of Java programs. Java 10 is also optimized for container integration, with the JVM choosing the number of CPU cores and memory footprint based on the container configuration. There are other low-level optimization features that I won’t talk about here, but you’ll know them when you reach a certain level. That concludes some of the Java 10 changes, which are not very many and are easy to grasp. Stay tuned, and next time we’ll summarize some of the changes and improvements in Java 11.