look again, unlimited power . searched " Program " and wrote the article seriously.

This article Github.com/niumoo/JavaNotes and unread code blog has been included, there are many knowledge points and series of articles.

<!--
图片来自 infoq.cn
-->
Java 15 was released in September 2020. Although it is not a long-term support version, it also brings 14 new features, many of which are very useful.

Java 15 official download: https://jdk.java.net/archive/

Java 15 official document: https://openjdk.java.net/projects/jdk/15/

<!-- more -->

New features in Java 15:

JEPdescribe
JEP 339 Edward Curve Algorithm (EdDSA)
JEP 360 Sealed Classes preview
JEP 371 Hidden Classes
JEP 372 Remove the Nashorn JavaScript engine
JEP 373 DatagramSocket APII
JEP 374 Disable and obsolete Biased Locking
JEP 375 instanceof type matching (second preview)
JEP 377 ZGC: Scalable low-latency garbage collector (officially released)
JEP 378 text block
JEP 379 Shenandoah: Garbage collector with low pause time
JEP 381 Delete Solaris and SPARC ports
JEP 383 external memory access API (the second incubator))
JEP 384 Records (Second Preview)
JEP 385 Abandoned RMI activation mechanism

1. JEP 339 Edward Curve Algorithm (EdDSA)

A new cryptographic algorithm has been added to Java 15, the Edward Curve Algorithm (EdDSA) signature algorithm. It is developed from the Schnorr algorithm and is defined and implemented 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));
    }
}

Output result:

VXlpxapU+LSWjVQ0QNJvdpUh6VI6PjSwOQ2pHu65bCfnLR13OyWKunlc9rc+7SMxCh2Mnqf7TmC/iOG8oimbAw==

2. JEP 360: Sealed Classes preview

We all know that if you want a class in Java that cannot be inherited or modified, then we should use the final keyword to modify the class. However, this mechanism of either inheritance or non-inheritance is not flexible enough. Sometimes we may want a certain class to be inherited by certain types, but it cannot be inherited arbitrarily, which is impossible. Java 15 tries to solve this problem by introducing the sealed class. sealed can be subclassed. In this way, this class can only be inherited by the specified class.

Moreover sealed is transitive, and its subclasses must be modified with specified keywords, and can only be one of final , sealed , and non-sealed .

Example: Dogs can only be inherited by Collie and TuGou, using the sealed keyword.

package com.wdbyte;

public  sealed interface Dog permits Collie, TuGou {
    //...
}

Collie can only be inherited by Border Collie.

package com.wdbyte;

/**
 * 牧羊犬
 * @author www.wdbyte.com
 */
public sealed class Collie implements Dog permits BorderCollie {

}

Border Collie (BorderCollie) cannot be inherited, use the final keyword.

package com.wdbyte;

/**
 *
 * @author www.wdbyte.com
 */
public final class BorderCollie extends Collie{
}

ToGou can be inherited arbitrarily, using the non-sealed keyword.

package com.wdbyte;

/**
 * @author niulang
 */
public non-sealed class TuGou implements Dog {
}

3. JEP 371: Hidden Classes (Hidden Classes)

This feature allows developers to introduce a class that cannot be found and used elsewhere and has a limited life cycle. This is very beneficial to the use of dynamically generated classes at runtime and can reduce memory usage. The following is an example of use.

package com.wdbyte;

public class JEP371Test {
    public static String lookup() {
      return "www.wdbyte.com";
    }
}

JEP371Test compiled Class Base64 to 0611da61735cdb, and then use the new features of Java 15 to load and call the lookup method in the 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+AQADKClWCAAIAQAOd3d3LndkYnl0ZS5jb20HAAoBABVjb20vd2RieXRlL0pFUDM3MVRlc3QBAARDb2RlAQAPTGluZU51bWJlclRhYmxlAQASTG9jYWxWYXJpYWJsZVRhYmxlAQAEdGhpcwEAF0xjb20vd2RieXRlL0pFUDM3MVRlc3Q7AQAGbG9va3VwAQAUKClMamF2YS9sYW5nL1N0cmluZzsBAApTb3VyY2VGaWxlAQAPSkVQMzcxVGVzdC5qYXZhACEACQACAAAAAAACAAEABQAGAAEACwAAAC8AAQABAAAABSq3AAGxAAAAAgAMAAAABgABAAAAAwANAAAADAABAAAABQAOAA8AAAAJABAAEQABAAsAAAAbAAEAAAAAAAMSB7AAAAABAAwAAAAGAAEAAAAEAAEAEgAAAAIAEw==";

    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);
    }
}

Output result:

com.wdbyte.JEP371Test/0x0000000800c01800
www.wdbyte.com

4. JEP 372: Remove the Nashorn JavaScript engine

The Nashorn JavaScript engine was introduced in Java 8 and was marked as obsolete in Java 11. Due to the rapid development of the ECMAScript language, the cost of maintaining Nashorn JavaScript was too high and was completely deleted in Java 15.

Extended reading: Nashorn JavaScript Engine , Deprecate the Nashorn JavaScript Engine

5. JEP 373: Reimplement the DatagramSocket API

The old Socket API was re-implemented in Java 13, and there was a part of this introduction when Java 13 was introduced.

Now, Java 15 re-implements the legacy DatagramSocket .

Extended reading: Java 13 New Features Introduction

6. JEP 374: Disabling and abolishing Biased Locking

Before, the JVM had a set of lock upgrade mechanisms when processing synchronization operations, such as the use of synchronized synchronization. One of the lock mechanisms was a biased lock. However, from the current Java development environment, synchronized synchronized by 0611da61735e37. For example, developers prefer to use HashMap or ArrayList instead of HashTable and Vector .

Even from another perspective, the original use of bias locks was to improve performance, but now it seems that the degree of performance improvement and the number of uses are not very useful. The introduction of biased locks increases the complexity of the JVM.

So now the bias lock is disabled by default and will be completely removed in the near future. For Java 15, we can still use -XX:+UseBiasedLocking enable the bias lock, but it will prompt that this is a deprecated API.

7. JEP 375: instanceof type matching (second preview)

instanceof type matching has been improved in Java 14, this time it is only previewed again, without any changes, to receive more feedback. This feature became an official feature in Java 16.

Previously, after using instanceof for type judgment, it was necessary to perform object type conversion before it could be used.

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);
    }
}

In Java 14, you can specify the variable name for type conversion when judging the type, which is convenient for use.

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);
    }
}

As you can see, after using instanceof determine that the type is established, the type will be automatically forced to be the specified type.

Output result:

[www.wdbyte.com]
Extended reading: Java 14 New Features Introduction

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 decided to introduce it gradually. Then continue to listen to user feedback and suggestions to fix the problem. Now, it has been a long time since we received any feedback from users, and it is time for ZGC to be put into use. So ZGC is officially released in Java 15, you can use the following parameters to enable ZGC.

$ java -XX:+UseZGC className

9. JEP 378: Text Block

The text block was introduced in Java 12 JEP 326 original string literal , in Java 13 JEP 355: text block (preview) began to preview, in Java 14 JEP 368: text block (second preview) , And now, in Java 15, text blocks are an official feature.

String content = """
        {
            "upperSummary": null,\
            "sensitiveTypeList": null,
            "gmtModified": "2011-08-05\s10:50:09",
        }
         """;
System.out.println(content);
Extended reading: Java 14 new function introduction-JEP368 text block

10. JEP 379: Shenandoah: Garbage Collector with Low Pause Time

The Shenandoah garbage collector was introduced in Java 12 and became part of the official function in Java 15. You can use the following parameters to enable the Shenandoah garbage collector.

java -XX:+UseShenandoahGC

But there is no Shenandoah collector by default in openJDK 15. If you want to use this function, you can download AdoptOpenJDK .

Why is there no Shenandoah garbage collector in openJDK?

Shenandoah is a high-performance, low-pause garbage collector, it is a project led by Red Hat. When Red Hat first proposed to contribute Shenandoah to OpenJDK, Oracle made it clear that it did not want to support it. As a free software, OpenJDK does not want to support Red Hat's Shenandoah.

In the end, Red Hat chose to work with Oracle to design a truly clean pluggable garbage collector interface, allowing anyone to easily choose a garbage collector to include in their build. Eventually Shenandoah entered JDK 12, but it was not built into OpenJDK.

11. JEP 384: Records (second preview)

The Record class was introduced in Java 14, and Record was enhanced in Java 15. It can support sealed types, Record annotations, and related reflection APIs.

Example: Record supports 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() implements DataBase {
}

In java.lang.Class two public methods are added to obtain Record class information:

  1. RecordComponent[] getRecordComponents()
  2. boolean isRecord()

Other updates

JEP 381: Remove Solaris and SPARC port

Java 14 JEP 362 deprecated Solaris/SPARC, Solaris/x64 and Linux/SPARC ports, and it is now officially deleted in Java 15.

JEP 383: External memory access API (the second incubator)

JEP 385:

It just discards the RMI activation mechanism and does not affect other functions of RMI.

refer to

  1. https://openjdk.java.net/projects/jdk/15/
  2. https://docs.oracle.com/en/java/javase/14/docs/specs/rmi/activation.html
  3. https://mkyong.com/java/what-is-new-in-java-15/

subscription

Hello world:) I am Aran, a first-line technical tool person, and write articles seriously.

1611da617362a0 likes likes 1611da617362a1 is a talent.

The article is continuously updated. You can follow the public Program Alang " or visit " Unread Code Blog ".

Reply 【Information】I have various series of knowledge points and must-read books that I have prepared.

This article Github.com/niumoo/JavaNotes has been included, there are many knowledge points and series of articles, welcome to Star.

等你好久


程序猿阿朗
376 声望1.8k 粉丝

Hello world :)