Make writing a habit together! This is the 13th day of my participation in the “Gold Digging Day New Plan · April More Text Challenge”. Click here for more details.
Java8 was released on March 18, 2014. As of April 6, 2022, the current latest release is Java18. Versions 17, 11, and 8 are the long Term Support (LTS) versions currently supported. This article takes you through the features of each version of Java from 8 onwards. Sit back and go! Want to have a crush on an article, click here to interview the kill | please discuss Java8-18 of the new features introduced (5)
New features in Java 16
Stream.toList()
From Java 16, we can collect results directly from the stream into a list without using Collectors. ToList ().
List<Integer> collectedList = locList.stream().filter( l -> l >=2).collect(Collectors.toList());
Copy the code
The above code will produce the same result as given below:
List<Integer> collectedListNew = locList.stream().filter( l -> l >=2).toList();
Copy the code
Invoke Default Methods From Proxy Instances
As an enhancement to the default methods in Interfaces, java.lang.Reflect is supported with the release of Java 16. The InvocationHandler invokes the default methods of the interface by using a reflected dynamic proxy.
Let’s look at a simple default method example:
interface HelloWorld { default String hello() { return "world"; }}Copy the code
With this improvement, we can use reflection to call the default proxy method of the interface:
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");
Copy the code
Day Period Support
A new addition to the date-time 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");Copy the code
Instead of “3 PM”, we get “3 PM”. We can also use the “B”, “BBBB”, or “BBB” DateTimeFormatter modes to handle short, full, and narrow styles, respectively.
There are other new features, including but not limited to:
- Vector API Incubator
- New Additions to Records
New features in Java 17
Java17 is a long term support (LTS) release, available at General Availability on September 14, 2021, with 14 JEP entries.
JEP 306: Restore Always-Strict Floating-Point Semantics
This JEP is primarily used for numerically sensitive programs, primarily for scientific purposes; Again, it makes the default floating-point operations strict, or Strictfp, to ensure that floating-point calculations on every platform yield the same results.
Since Java 1.2, we have required the keyword STRICtFP to enable strict floating-point calculations. The default floating-point calculation was changed from strictly to a slightly different floating-point calculation (to avoid overheating problems).
Java17 reverts the pre-java 1.2 strict floating-point calculation to the default, which means that the keyword strictfp is now optional.
JEP 356: Enhanced Pseudo-Random Number Generators
This JEP introduces a new interface called RandomGenerator that will make future PRNG algorithms easier to implement and use. The pseudo-random number generator for this interface is:
package java.util.random;
public interface RandomGenerator {
//...
}
Copy the code
The following example uses the new java17 randomizer factory to obtain the famous Xoshiro256PlusPlus PRNG algorithm to generate random integers in a specific range, 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++; }}}Copy the code
The following code generates all of the Java 17 PRNG algorithms.
RandomGeneratorFactory.all()
.map(fac -> fac.group()+ " : " +fac.name())
.sorted()
.forEach(System.out::println);
Copy the code
17 also refactored legacy Random classes such as java.util. Random, SplittableRandom, and Securierandom to extend the new Random generator interface.
JEP 382: New macOS Rendering Pipeline
Apple did not recommend the OpenGL rendering library in the macOS 10.14 release (September 2018), instead supporting the new Metal framework for better performance.
This JEP changes the macOS java2D (such as Swing GUI) internal rendering pipeline from Apple OpenGL API to Apple Metal API; This is an internal change; There are no new Java2D apis and no changes to any existing apis.
JEP 409: Sealed Classes
Java 15 and Java 16 have introduced Sealed Classes as a preview feature. The current JEP declares closed classes to be a standard feature. Sealed classes and interfaces control or limit who can be subtypes.
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 continue, the following will continue to talk about the new features of each version, stay tuned! The final chapter, Java 18, was released on March 22, 2022.