Send whatever you want, I’ll use Java8. This article has been https://www.yourbatman.cn included, along with all the inside have Spring technology stack, MyBatis, middleware, such as the JVM, small and beautiful column study for free. Pay attention to the public number [BAT utopia] one by one to break, in-depth grasp,

The foreword ✍

JDK15 was officially launched on September 15, 2020, as promised. Following the Java SE roadmap, UPDATES to JDK14 have been discontinued since then. It is important to note that JDK15 is notLTSOracle’s official Java SE support roadmap is as follows:JDK8 extended support is longer than JDK11, Oracle are you serious? Just kidding

So which version is the LTS version since Java11? Oracle doesn’t provide a roadmap, but check out this one from OpenJDK:You can seeJDK17Will be the next LTS release with an expected release date of September 2021. Of course, this is only the OpenJDK release map, does not represent the official Oracle, so it is only for reference, but generally it is similar.

Tip: The OpenJDK and Oracle JDK have shared most of their code since JDK11.

Since JDK9, Oracle has adopted a new release cycle: release every 6 months and LTS release every 3 years. JDK14 is the fourth non-LTS release after JDK9. The latest LTS release is JDK11. Because it’s a deer running and iterating fast, I’ll explain the words Incubator and Preview.

Incubator Module (Incubation version/experimental version)

The API/ tool that has not been finalized is mainly used to collect usage feedback from the Java community. Stability is not guaranteed, and it may be removed later

Preview features (Preview version)

The specifications are in shape, the implementation is determined, but not yet finalized. These features can still be removed, but are generally fixed eventually.

✍ body

JDK15 is the 15th version of the Java SE platform, specified by JSR 390 in the Java community process.

OpenJDK 15 was released 9-15, Oracle sync up. Corresponding JDK versions from other vendors will follow

Fourteen new features are available in this release, represented by these Jeps, as shown below:The following is an explanation of the features that are important to the daily programming of developers, and an example of their use (in fact, JEP 378).

Review of new features in JDK14

As always, before introducing the new features of JDK15, let’s review the main features of JDK14. JDK 14 was released on March 17, 2020.

Switch expression

The new Switch expressions are already available in JDK 12 and 13, but they are only preview versions. By JDK 14, they are completely stable and can be used commercially.

Tip: Preview features can be removed in later releases, but are almost impossible to remove in stable releases

The new expression for switch has two notable features:

  • Support for arrow expression returns
  • Yield and return values are supported.

1. Arrow expression returns

JDK14 previously written:

private static void printLetterCount(DayOfWeek dayOfWeek){
    switch (dayOfWeek) {
        case MONDAY:
        case FRIDAY:
        case SUNDAY:
            System.out.println(6);
            break;
        case TUESDAY:
            System.out.println(7);
            break;
        case THURSDAY:
        case SATURDAY:
            System.out.println(8);
            break;
        case WEDNESDAY:
            System.out.println(9);
            break; }}Copy the code

Important: Do not forget to write break, otherwise it is a big bug, and also relatively hidden, slightly difficult to locate.

New version of JDK14 equivalent:

private static void printLetterCount(DayOfWeek dayOfWeek){
    switch (dayOfWeek) {
        case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);
        case TUESDAY                -> System.out.println(7);
        case THURSDAY, SATURDAY     -> System.out.println(8);
        case WEDNESDAY              -> System.out.println(9); }}Copy the code

It is obvious that the new writing method does not need to break one by one, which avoids the possibility of making mistakes from the grammatical level.

2. Yield returns

JDK14 previously written:

private static int getLetterCount(DayOfWeek dayOfWeek){
    int letterCount;
    switch (dayOfWeek) {
        case MONDAY:
        case FRIDAY:
        case SUNDAY:
            letterCount = 6;
            break;
        case TUESDAY:
            letterCount = 7;
            break;
        case THURSDAY:
        case SATURDAY:
            letterCount = 8;
            break;
        case WEDNESDAY:
            letterCount = 9;
            break;
        default:
            throw new IllegalStateException("Illegal:" + dayOfWeek);
    }
    return letterCount;
}
Copy the code

New version of JDK14 equivalent:

private static int getLetterCount(DayOfWeek dayOfWeek){
    return switch (dayOfWeek) {
        case MONDAY, FRIDAY, SUNDAY -> 6;
        case TUESDAY                -> 7;
        case THURSDAY, SATURDAY     -> 8;
        case WEDNESDAY              -> 9;
    };
}
Copy the code

The effects of using the arrow operator are immediate. Of course, you can also use the yield keyword to return:

private static int getLetterCount(DayOfWeek dayOfWeek){
    return switch (dayOfWeek) {
        case MONDAY  -> 6;
        default- > {intletterCount = dayOfWeek.toString().length(); yield letterCount; }}; }Copy the code

Ii. Pattern Matching of Instanceof (preview)

This feature is handled in the preview version in JDK14.

JDK14 previously written:

public static void main(String[] args) {
    Object o = "hello world";
    if(o instanceofString ){ String str = String.class.cast(o); System.out.println(str); }}Copy the code

New version of JDK14 equivalent:

public static void main(String[] args) {
    Object o = "hello world";
    // You can write a variable name in the bottom
    if(o instanceofString str){ System.out.println(str); }}if (obj instanceof String s && s.length() > 5) {
	s.contains(..)
}
Copy the code

If you run the following error:

java: instanceofPattern matching is a preview feature, disabled by default. (Use --enable-preview to enable itinstanceofPattern matching inCopy the code

That’s because this feature isThe preview feature, you need to take the initiative to open, as follows:Note: This feature is available inJDK15Is still a preview version.

NullPointerException

A little.

Four, Record (preview)

Java is old, the syntax isn’t fancy, and sometimes it’s just too cumbersome, hence Record: killing get/set, toString, equals, etc.

public record Person(String name,Integer age) {}public static void main(String[] args) {
    Person person= new Person("YourBatman".18);
    System.out.println(person);
    System.out.println(person.name());
    System.out.println(person.age());
}
Copy the code

Run the program and print the result:

Person[name=YourBatman, age=18]
YourBatman
18
Copy the code

Note: This feature is still available as a preview in JDK15.

Text Blocks (second preview)

This feature is very useful and is a secondary preview: it was previewed once in JDK 13.

public static void main(String[] args) {
    String html = """   

hello world

"
""; String query = """ SELECT * from USER WHERE `id` = 1 ORDER BY `id`, `name`; """; } Copy the code

In JDK13, this has line breaks. In JDK14, we can add a symbol to prevent line breaks:

public static void main(String[] args) {
    String query = """ SELECT * from USER \ WHERE `id` = 1 \ ORDER BY `id`, `name`; \""";
    System.out.println(query);
}
Copy the code

Run the program, and the output (as you can see it is displayed as a line) :

SELECT * from USER WHERE `id` = 1 ORDER BY `id`, `name`;
Copy the code

Note: This feature is official in JDK15.

Delete the CMS garbage collector

The famous garbage collector has been completely removed from this release. When JDK9 started using G1 as the default garbage collector (ZGC in JDK11 came to the scene), CMS was marked as expired and officially removed in this release.

7. ZGC garbage Collector (Experiment)

Revolutionary ZGC: Any heap size (terabyte level) can guarantee latency less than 10ms, is a garbage collector with low latency as the primary goal.

Prior to JDK14, ZGC was only available on Linux, now it is available on Windows as well

Note: This feature is official in JDK15 (it has been available since JDK11).


JDK15 new features

With a review of the new JDK14 features set in place, it’s easy to look at the new JDK15 features.

IDEA 2020.2 is required to run JDK15. IDEA 2020.2 is required to run JDK15. IntelliJ IDEA 2020.2 is officially released. There are always a few highlights to help you improve the efficiency

Text Blocks

Text Blocks were first previewed in JDK 13, previewed again in JDK 14, and finally identified in JDK 15 (see the usage examples above).

Second, ZGC turns positive

ZGC is a new garbage collector introduced in Java 11 (the default garbage collector from JDK9 onwards is G1) and has gone through several phases of experimentation before finally becoming an official feature.

ZGC is a redesigned concurrent garbage collector that can greatly improve GC performance. Support any heap size while maintaining stable low latency (less than 10ms), performance is very impressive.

Open it with the -xx :+UseZGC command line argument. It will be the default garbage collector in the near future.

Shenandoah becomes a full-time official

How do you describe Shenandoah’s relationship with the ZGC? The similarities and differences are as follows:

  • Similarities: Performance can be considered almost identical
  • Difference: ZGC is the Oracle JDK, root is red. Shenandoah only exists in the OpenJDK, so be aware of your JDK version when using it

Open method: Use the -xx :+UseShenandoahGC command-line argument.

Delete Nashorn JavaScript Engine

Nashorn is a script execution engine introduced in JDK11, which was marked as expired in JDK11 and removed completely.

The alternative in JDK11 is GraalVM. GraalVM is a runtime platform that supports Java and other Java bytecode based languages, but also supports other languages such as JavaScript, Ruby, Python, or LLVM. More than twice the performance of Nashorn.

5. Add isEmpty method to CharSequence

Nothing to say, a look at the source will know:

@since 15
default boolean isEmpty(a) {
    return this.length() == 0;
}
Copy the code

String implements the CharSequence interface.

Upgrade Suggestions

Just play with it, it’s not the LTS version.

However, although said to be limited to their own play on the line, but does not mean that there is no focus on the significance of Ha. Again, if JDK12, 13, 14, 15… If you don’t pay attention to it, it will be a little difficult to accept the LTS version of JDK17.

✍ summary

JDK15 doesn’t have a lot of new features as a whole, it’s mostly a confirmation of the preview features of previous versions, such as text blocks, ZGC, etc., so that we can use them with ease.

Half a year of the version of the speed really learn not to move, but fortunately I have my insistence: you send you send, I use Java8.

IDEA2020.2.2 + JDK15 installation package (MAC + Windows)

  • IntelliJ IDEA 2020.2 is officially released. There are always a few highlights to help you improve the efficiency
  • Spring Boot 2.3.0: Graceful shutdown, configuration file location wildcard features
  • Make things? Spring Boot launches three versions today