This is the sixth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
Like and see, unlimited power. Wechat search “program ape Arang”, serious writing articles.
This article Github.com/niumoo/Java… And the unread Code blog, with many knowledge points and a series of articles.
Java 15 was released in September 2020, and while not a permanent support release, it brings 14 new features, many of which are very useful.
Java 15 official download: jdk.java.net/archive/
Java 15 official documentation: openjdk.java.net/projects/jd…
Java 15 new features:
JEP | describe |
---|---|
JEP 339 | Edward curve algorithm (EdDSA) |
JEP 360 | Sealed Classes preview |
JEP 371 | Hidden Classes |
JEP 372 | Remove Nashorn JavaScript engine |
JEP 373 | Re-implement DatagramSocket APII |
JEP 374 | Disable and discard Biased Locking |
JEP 375 | Instanceof Type matching (second preview) |
JEP 377 | ZGC: Scalable Low-latency Garbage Collector (official release) |
JEP 378 | Text block |
JEP 379 | Shenandoah: Low pause time garbage collector |
JEP 381 | Delete Solaris and SPARC ports |
JEP 383 | External memory Access API (second incubator) |
JEP 384 | Records (second preview) |
JEP 385 | Discard the RMI activation mechanism |
1. JEP 339 Edward Curve Algorithm (EdDSA)
Java 15 has added a new cryptography algorithm, the Edwards-curve algorithm (EdDSA) signature algorithm. It is developed from Schnorr algorithm and is defined in RFC8032.
package com.wdbyte;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.Signature;
import java.security.SignatureException;
import java.util.Base64;
public class JEP339 {
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("Ed25519");
KeyPair kp = kpg.generateKeyPair();
byte[] msg = "www.wdbyte.com".getBytes(StandardCharsets.UTF_8);
Signature sig = Signature.getInstance("Ed25519");
sig.initSign(kp.getPrivate());
sig.update(msg);
byte[] s = sig.sign(); System.out.println(Base64.getEncoder().encodeToString(s)); }}Copy the code
Output results:
VXlpxapU+LSWjVQ0QNJvdpUh6VI6PjSwOQ2pHu65bCfnLR13OyWKunlc9rc+7SMxCh2Mnqf7TmC/iOG8oimbAw==
Copy the code
2. JEP 360: Sealed Classes
We all know that in Java, if we want a class to be uninherited and unmodified, we should use the final keyword to decorate the class. However, this either inherit or not inheritance mechanism is not flexible, sometimes we may want a class to be inherited by certain types, but not arbitrary inheritance, it is not possible. Java 15 tries to solve this problem by introducing the Sealed class, which can specify subclasses. This class can only be inherited by the specified class.
In addition, the mechanism of sealed class is transitive. Its subclasses must be modified with the specified keyword and can only be one of final, sealed, and non-sealed.
Example: Dog can only be inherited by Collie and TuGou, using the sealed keyword.
package com.wdbyte;
public sealed interface Dog permits Collie.TuGou {
/ /...
}
Copy the code
A Collie can only be inherited by a BorderCollie.
package com.wdbyte;
/** * Sheepdog *@author www.wdbyte.com
*/
public sealed class Collie implements Dog permits BorderCollie {}Copy the code
BorderCollie cannot be inherited. Use the final keyword.
package com.wdbyte;
/ * * * *@author www.wdbyte.com
*/
public final class BorderCollie extends Collie{}Copy the code
Pastoral dog (ToGou) can be inherited at will, using the non-sealed keyword.
package com.wdbyte;
/ * * *@author niulang
*/
public non-sealed class TuGou implements Dog {}Copy the code
3. JEP 371: Hidden Classes
This feature allows developers to introduce a class that cannot be found and used elsewhere and has a limited lifetime. This is a useful way to use dynamically generated classes at runtime to reduce memory footprint, as shown in the following example.
package com.wdbyte;
public class JEP371Test {
public static String lookup(a) {
return "www.wdbyte.com"; }}Copy the code
Convert the compiled Class of the JEP371Test Class to Base64, and then use the new Java 15 features to load the lookup method in the calling Class.
package com.wdbyte;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Base64;
/ * * *@author www.wdbyte.com
*/
public class JEP371 {
private static String CLASS_INFO = "yv66vgAAADQAFAoAAgADBwAEDAAFAAYBABBqYXZhL2xhbmcvT2JqZWN0AQAGPGluaXQ+AQADKClWCAAIAQAOd3d3LndkYnl0ZS5jb20HAAoBABVjb20vd2R ieXRlL0pFUDM3MVRlc3QBAARDb2RlAQAPTGluZU51bWJlclRhYmxlAQASTG9jYWxWYXJpYWJsZVRhYmxlAQAEdGhpcwEAF0xjb20vd2RieXRlL0pFUDM3MVR lc3Q7AQAGbG9va3VwAQAUKClMamF2YS9sYW5nL1N0cmluZzsBAApTb3VyY2VGaWxlAQAPSkVQMzcxVGVzdC5qYXZhACEACQACAAAAAAACAAEABQAGAAEACwA AAC8AAQABAAAABSq3AAGxAAAAAgAMAAAABgABAAAAAwANAAAADAABAAAABQAOAA8AAAAJABAAEQABAAsAAAAbAAEAAAAAAAMSB7AAAAABAAwAAAAGAAEAAAA EAAEAEgAAAAIAEw==";
public static void main(String[] args) throws Throwable {
byte[] classInBytes = Base64.getDecoder().decode(CLASS_INFO); Class<? > proxy = MethodHandles.lookup() .defineHiddenClass(classInBytes,true, MethodHandles.Lookup.ClassOption.NESTMATE)
.lookupClass();
System.out.println(proxy.getName());
MethodHandle mh = MethodHandles.lookup().findStatic(proxy, "lookup", MethodType.methodType(String.class)); String result = (String) mh.invokeExact(); System.out.println(result); }}Copy the code
Output results:
com.wdbyte.JEP371Test/0x0000000800c01800
www.wdbyte.com
Copy the code
4. JEP 372: Nashorn JavaScript engine removed
The Nashorn JavaScript engine was introduced in Java 8 and marked obsolete in Java 11. Nashorn JavaScript was too expensive to maintain because the ECMAScript language was growing quickly and was removed completely in Java 15.
Nashorn JavaScript Engine will be deprecated in the future
JEP 373: Re-implement the DatagramSocket API
The old Socket API was reimplemented in Java 13 and was partially covered in the introduction to Java 13.
Now, Java 15 reimplements the legacy DatagramSocket.
Extended reading: Introduction to new Features of Java 13
6. JEP 374: Disabled and Abandoned Biased Locking
Previously, the JVM had a set of lock escalation mechanisms when dealing with synchronization operations such as synchronized, one of which was biased locking. However, given the current Java development environment, there are few opportunities to use these synchronized classes, as developers prefer to use HashMap or ArrayList rather than HashTable and Vector.
Even if, on the other hand, bias locking was originally used to improve performance, it seems that the degree of performance improvement and the number of times it is used is not very useful. The introduction of bias locking adds complexity to the JVM.
So bias locking is now disabled by default and will be removed completely in the near future. For Java 15, we can still enable bias locking using -xx :+UseBiasedLocking, but it will indicate that this is a deprecated API.
7. JEP 375: Instanceof Type Matching
Instanceof type matching has been improved in Java 14, and this time it will only be previewed again, without any changes, to receive more feedback on usage. This feature became official in Java 16.
Previously, after using instanceof for type determination, you need to cast the object before using it.
package com.wdbyte;
import java.util.ArrayList;
import java.util.List;
public class Java14BeaforInstanceof {
public static void main(String[] args) {
Object obj = new ArrayList<>();
if (obj instanceof ArrayList) {
ArrayList list = (ArrayList)obj;
list.add("www.wdbyte.com"); } System.out.println(obj); }}Copy the code
In Java 14, you can specify the name of the variable to be cast when determining the type.
package com.wdbyte;
import java.util.ArrayList;
public class Java14Instanceof {
public static void main(String[] args) {
Object obj = new ArrayList<>();
if (obj instanceof ArrayList list) {
list.add("www.wdbyte.com"); } System.out.println(obj); }}Copy the code
As you can see, after using instanceof to determine that the type is true, the type is automatically cast to the specified type.
Output results:
[www.wdbyte.com]
Copy the code
Extended reading: Introduction to new Features of Java 14
8. JEP 377: ZGC: Scalable Low Latency Garbage Collector (official release)
The ZGC garbage collector was introduced in Java 11, but because of the complexity of the collector, it was initially decided to introduce it gradually. Then keep listening to user feedback and suggestions to fix the problem. Now, with no feedback from users for a long time, it’s time for ZGC to launch. So when ZGC is officially released in Java 15, you can enable ZGC with the following parameters.
$ java -XX:+UseZGC className
Copy the code
9. JEP 378: Text block
Text blocks are introduced in Java 12 JEP 326 raw string literals, preview begins in Java 13 JEP 355: Text blocks (preview), and preview begins in Java 14 JEP 368: Text blocks (second preview), which are now an official feature in Java 15.
String content = """{"upperSummary": null,\ "sensitiveTypeList": null, "gmtModified":"2011- 08 -05\s10:50: 09, "}""";
System.out.println(content);
Copy the code
Java 14 New features – JEP368 text block
10. JEP 379: Shenandoah: Low pause time garbage collector
The Shenandoah garbage collector was introduced in Java 12 and became part of the formal functionality in Java 15. You can enable the Shenandoah garbage collector using the following parameters.
java -XX:+UseShenandoahGC
Copy the code
By default, there is no Shenandoah collector in openJDK 15. To use this feature, download AdoptOpenJDK.
Why is there no Shenandoah garbage collector in openJDK?
Shenandoah is a high performance, low pause time garbage collector that is a Red Hat-led project. When Red Hat first proposed to contribute Shenandoah to OpenJDK, Oracle made it clear that it didn’t want to support it, and as OpenJDK is a free piece of software, it’s perfectly fine not to want to support Red Hat Shenandoah.
In the end, Red Hat chose to work with Oracle to design a truly clean pluggable garbage collector interface that would allow anyone to easily select a garbage collector to include in their build. Eventually Shenandoah got into JDK 12, but didn’t build into OpenJDK.
11. JEP 384: Records (second preview)
The Record class was introduced in Java 14 and enhanced in Java 15. Make it possible to support seal types, Record annotations, and related reflection apis.
Example: Record supports the sealed type.
package com.wdbyte;
/ * * *@author www.wdbyte.com
*/
public sealed interface DataBase permits DataBaseSelect.DataBaseUpdate {}final record DataBaseSelect(@Deprecated String table, String sql) implements DataBase {}final record DataBaseUpdate(a) implements DataBase {}Copy the code
Added two public methods to java.lang.Class to retrieve information about the Record Class:
-
RecordComponent[] getRecordComponents()
-
boolean isRecord()
Other updates
JEP 381: Remove Solaris and SPARC ports
Java 14 JEP 362 deprecated Solaris/SPARC, Solaris/ X64, and Linux/SPARC ports, and it is now officially removed in Java 15.
JEP 383: External Memory Access API (second incubator)
JEP 385: Discard the RMI activation mechanism
The RMI activation mechanism is discarded and other RMI functions are not affected.
reference
- Openjdk.java.net/projects/jd…
- Docs.oracle.com/en/java/jav…
- Mkyong.com/java/what-i…
series
-
Introduction to new Java 15 features
-
Java 14 new features
-
Java 13 new features
-
Introduction to new Java 12 features
-
Introduction to new Java 11 features
-
Introduction to new Java 10 features
-
Java 9 new features
-
Java 8 function interface UnaryOperator
-
Java 8 function interface BiPredicate
-
Java 8 Function interface BiFunction
-
Java 8 function interface Supplier
-
Java 8 function interface Predicate
-
Java 8 Function interface Function
-
New in Java 8 – forEach traversal
-
Java 8 New features – LocalDate, LocalDateTime time processing introduction
-
New in Java 8 – Use Optional elegance to handle null Pointers
-
Java 8 new features – Lambda expressions, function interfaces to understand
-
Java 8 new feature – super Stream manipulation posture is not yet learned
-
New Java 7 features – Say goodbye to inefficient IO, Files,Paths, and Paths
-
New Java 7 Features – New Features – Here are some new Java 7 syntax features
To subscribe to
Hello World:) I am Aaron, a first-line technology tool person, serious writing.
All of them are talents, not only handsome and good-looking, but also nice to hear.
Articles continue to update, you can follow the public number “program ape Arang” or visit the “unread code blog”.
There are a series of knowledge points and must-see books prepared by me.
This article Github.com/niumoo/Java… Have been included, there are many knowledge points and series of articles, welcome Star.