Interview to kill | Please talk about the new features introduced by Java8-18 (6)

Get into the habit of writing together! This is the 13th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the event details .

Java8 was released on March 18, 2014, and as of April 6, 2022, the latest release is Java18. Versions 17, 11 and 8 are the currently supported Long Term Support (LTS) releases. This article leads you to review the features of each version starting from Java 8. Sit on the bench and go! If you want to read the previous article, click here for an interview and anti-kill | Please talk about the new features introduced by Java8-18 (5)

What's New in Java 16

Stream.toList()

From Java 16, we can directly collect the results from the stream into a list instead of using Collectors.toList().

List<Integer> collectedList = locList.stream().filter( l -> l >=2).collect(Collectors.toList());
复制代码

The above code will produce the same result as given below:

List<Integer> collectedListNew = locList.stream().filter( l -> l >=2).toList();
复制代码

Invoke Default Methods From Proxy Instances

As an enhancement to the default methods in Interfaces, with the release of Java 16, Java.lang.reflect is supported. The InvocationHandler invokes the default methods of the interface by using a dynamic proxy using reflection.

Let's take a look at a simple default method example:

interface HelloWorld {
    default String hello() {
        return "world";
    }
}
复制代码

With this improvement, we can use reflection to call the default method of this interface's proxy:

Object proxy = Proxy.newProxyInstance(getSystemClassLoader(), new Class<?>[] { HelloWorld.class },
    (prox, method, args) -> {
        if (method.isDefault()) {
            return InvocationHandler.invokeDefault(prox, method, args);
        }
        // ...
    }
);
Method method = proxy.getClass().getMethod("hello");
assertThat(method.invoke(proxy)).isEqualTo("world");
复制代码

Day Period Support

A new addition to the datetime formatter, 'b', which provides an alternative to the am/pm format:

LocalTime date = LocalTime.parse("15:25:08.690791");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h B");
assertThat(date.format(formatter)).isEqualTo("3 in the afternoon");
复制代码

Instead of "3pm" we get "3pm". We can also use the 'b', 'BBBB' or 'bbb' DateTimeFormatter patterns respectively for short, full and narrow styles.

Of course there are other new features, including but not limited to:

  • Vector API Incubator
  • New Additions to Records

What's New in Java 17

Java 17 is a Long Term Support (LTS) release, reaching General Availability on September 14, 2021, with 14 JEP entries.

image.png

JEP 306: Restore Always-Strict Floating-Point Semantics

这个 JEP 主要用于数值敏感的程序,主要用于科学目的; 它再次使默认的浮点操作变得严格,或者叫做 Strictfp,以确保每个平台上的浮点计算得到相同的结果。

自 Java 1.2以来,我们需要关键字 strictfp 来启用严格的浮点计算。默认的浮点计算从严格改为略微不同的浮点计算(避免过热问题)。

Java17将前 java1.2严格的浮点计算恢复为默认值,这意味着关键字 strictfp 现在是可选的。

JEP 356: Enhanced Pseudo-Random Number Generators

这个 JEP 引入了一个叫做 RandomGenerator 的新接口,使得未来的 PRNG 算法更容易实现和使用。这个接口的伪随机数生成器是:

package java.util.random;
​
public interface RandomGenerator {
  //...
}
复制代码

下面的示例使用新的 java17随机发生器工厂获得著名的 Xoshiro256PlusPlus PRNG 算法来生成特定范围内的随机整数,0-10。

package com.mine.java17.jep356;
​
import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;
​
public class JEP356 {
​
  public static void main(String[] args) {
​
      RandomGenerator randomGenerator = RandomGeneratorFactory.of("Xoshiro256PlusPlus").create(999);
​
      System.out.println(randomGenerator.getClass());
​
      int counter = 0;
      while(counter<=10){
          // 0-10
          int result = randomGenerator.nextInt(11);
          System.out.println(result);
          counter++;
      }
​
  }
}
复制代码

下面的代码生成所有的 Java 17 PRNG 算法。

  RandomGeneratorFactory.all()
              .map(fac -> fac.group()+ " : " +fac.name())
              .sorted()
              .forEach(System.out::println);
复制代码

17还重构了遗留的随机类,比如 Java.util.Random、 SplittableRandom 和 securierandom,以扩展新的随机生成器接口。

JEP 382: New macOS Rendering Pipeline

苹果在 macOS 10.14发布版(2018年9月)中不推荐 OpenGL 渲染库,而是支持新的 Metal 框架,以获得更好的性能。

这个 JEP 改变了 macOS 的 java2d (如 Swing GUI)内部呈现管道,从 Apple OpenGL API 改为 Apple Metal API; 这是一个内部改变; 没有新的 java2d API,也没有改变任何现有的 API。

JEP 409: Sealed Classes

Java 15 and Java 16 introduced Sealed Classes as a preview feature. The current JEP declares that enclosing classes have become a standard feature. Sealed classes and interfaces control or restrict who can be their subtypes.

Of course there are other new features, including but not limited to:

  • JEP 391: macOS/AArch64 Port
  • JEP 398: Deprecate the Applet API for Removal
  • JEP 403: Strongly Encapsulate JDK Internals
  • JEP 406: Pattern Matching for switch (Preview)
  • JEP 407: Remove RMI Activation
  • JEP 410: Remove the Experimental AOT and JIT Compiler
  • JEP 411: Deprecate the Security Manager for Removal
  • JEP 412: Foreign Function & Memory API (Incubator)

To be continued, let's continue to talk about the new features of each version, so stay tuned!
Final chapter, Java 18 released March 22, 2022.

Guess you like

Origin juejin.im/post/7085868668017442823