JDK15 was officially released, and the epoch-making ZGC also announced that

You can send whatever you want, I use Java8. This article has been https://www.yourbatman.cn Included, there are small and beautiful columns such as Spring technology stack, MyBatis, JVM, middleware, etc. for free learning. Pay attention to the public account [BAT's Utopia] to defeat them one by one, master them in depth, and refuse to try them out.

content

✍Foreword

On September 15, 2020, JDK15 was officially released, which can be said to arrive as promised. According to the development roadmap of Java SE, JDK14 has stopped updating since then. It is worth noting that JDK15 is not an LTS version. Oracle's official support roadmap for Java SE is as follows:

The extended support time of JDK8 exceeds that of JDK11. Are you serious about Oracle? Just kidding~

So since Java11, which version is the LTS version? Oracle officials did not give a specific reference roadmap, but you can refer to this one from OpenJDK:

It can be seen that JDK17 will be the next LTS version, and the expected release date is September 2021. Of course, this is just the release roadmap of OpenJDK, and it does not represent the official Oracle, so it is for reference only, but it is generally inseparable.

Tips: OpenJDK and Oracle JDK have shared most of the code since JDK11, and the rhythm is basically the same.

Since JDK9, Oracle has adopted a new release cycle: a release every 6 months and an LTS release every 3 years. JDK14 is the fourth version released after JDK9, which is a non-LTS version, and the latest LTS version is JDK11. Because it is a small deer running fast and iterating quickly, the two words are explained here: the incubator module (Incubator) and the preview feature (Preview).

Incubator Module (Incubator/Experimental)

API s/tools that have not yet been finalized are mainly used to collect usage feedback from the Java community. The stability is not guaranteed, and there is a high possibility of removing them later.

Preview Features (Preview)

Specifications are formed, implementation is finalized, but not yet finalized. There is still the possibility of these features being removed, but generally they will eventually be fixed.

✍Text

JDK15 is the 15th release of the Java SE platform, specified by JSR 390 in the Java Community Process.

OpenJDK 15 was released on 9-15, and Oracle kept up with it. The corresponding JDK versions of other manufacturers will also follow.

This version provides a total of 14 new features, which are represented by these JEP s. The screenshots are as follows:

The following is an explanation of the features that are closely related to the daily programming of developers, and the corresponding usage examples are given (in fact, it is JEP 378).

Review of new features of JDK14

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

1. Switch expression

The new Switch expression actually existed as early as JDK 12 and 13, but it is only a preview version. It will become a stable version in JDK 14 and can be used with confidence.

Tip: It is possible for preview features to be removed in subsequent releases, but it is almost impossible to remove them after the stable release

The new switch expression has two notable features:

  • Support arrow expression return
  • Supports yield and return return values.

1. Arrow expression returns

Writing before JDK14:

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

Point: Don't forget to write break, otherwise it will be a big bug, and it is relatively hidden, making it difficult to locate.

JDK14 equivalent new writing:

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

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

2. yield returns

Writing before JDK14:

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

JDK14 equivalent new writing:

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

Use the arrow operator to get instant results. Of course, you can also use the yield keyword to return:

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

2. Pattern matching of instanceof (preview)

The feature handles preview in JDK14.

Writing before JDK14:

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

JDK14 equivalent new writing:

public static void main(String[] args) {
    Object o = "hello world";
    // You can write a variable name directly in the ass, no need to force it anymore
    if(o instanceof String str){
        System.out.println(str);
    }
}

Another example:
if (obj instanceof String s && s.length() > 5) {
	s.contains(..)
}

If you run with the following error:

java: instanceof Pattern matching in is a preview feature, disabled by default.
  (please use --enable-preview to enable instanceof pattern matching in)

That's because this feature is a preview feature that requires you to actively enable it, as follows:

Note: This feature is still in preview in JDK15.

3. Practical NullPointerException

slightly.

4. Record (preview)

Java is too old, the syntax is not trendy enough, and sometimes it is too troublesome, so there is the emergence of Record: get rid of those get/set, toString, equals and other methods.

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

Run the program and print the result:

Person[name=YourBatman, age=18]
YourBatman
18

Note: This feature is still in preview in JDK15.

5. Text Blocks (secondary preview)

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

public static void main(String[] args) {
    String html = """
          <html>
              <body>
                  <p>hello world</p>
              </body>
          </html>
          """;
    String query = """
           SELECT * from USER
           WHERE `id` = 1
           ORDER BY `id`, `name`;
           """;
}

In JDK13, this is a newline. In JDK14, you can add a symbol to prevent it from wrapping:

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

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

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

Note: This feature has been officially released in JDK15.

6. Delete the CMS garbage collector

The famous garbage collector has been completely removed from this release. JDK9 began to use G1 as the default garbage collector (ZGC began to emerge in JDK11), and CMS has been marked as expired, and it is officially deleted in this version.

Seven, ZGC garbage collector (experimental)

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

Before JDK14, ZGC could only be used on Linux, and now it can also be used on windows

Note: This feature has been officially released in JDK15 (JDK11 began to appear).

New features of JDK15

With the review of the new features of JDK14 as a foreshadowing, it is much more convenient to understand the new features of JDK15.

Special note: IDEA 2020.2 is required to run JDK15 (JDK14 requires IDEA 2020.1), and then for the usage tutorial (new features) of IDEA 2020.2, please move to this article posted in front of my official account: IntelliJ IDEA 2020.2 is officially released, and there are always a few highlights that can help you improve your performance

1. Text Blocks

Text Blocks first appeared as a preview function in JDK 13, and then previewed again in JDK 14. Finally, it was confirmed in JDK 15 and can be used with confidence (please refer to the article for usage examples).

Second, ZGC positive

ZGC is a new garbage collector introduced in Java 11 (the default garbage collector after JDK9 is G1). After several experimental stages, it has finally become an official feature.

ZGC is a redesigned concurrent garbage collector that can greatly improve GC performance. Supports any heap size and maintains a stable low latency (within 10ms), and the performance is very impressive.

Open method: use -XX:+UseZGC command line parameter to open, I believe it will become the default garbage collector in the near future.

3. Shenandoah turned positive

How to describe the relationship between Shenandoah and ZGC? The similarities and differences are roughly as follows:

  • Similarities: The performance can be considered almost the same
  • The difference: ZGC is the Oracle JDK, and it is rooted in Miaohong. Shenandoah only exists in OpenJDK, so pay attention to your JDK version when using it

Open method: use -XX:+UseShenandoahGC command line parameter to open.

4. Delete Nashorn JavaScript Engine

Nashorn is a script execution engine proposed in JDK. It has been marked as expired as early as JDK11, and JDK15 is completely removed.

In JDK11 it was replaced by GraalVM. GraalVM is a runtime platform that supports Java and other Java bytecode based languages, but also other languages ​​such as JavaScript, Ruby, Python or LLVM. The performance is more than 2 times that of Nashorn.

5. CharSequence adds isEmpty default method

Don't say anything, you can see the source code at a glance:

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

String implements the CharSequence interface, which should be known to everyone on earth.

Upgrade suggestion

Just play it yourself, it is not the LTS version after all.

However, although it is said that it is limited to play by yourself, it does not mean that there is no meaning to pay attention. It's the same reason, if JDK12, 13, 14, 15... don't pay attention, then suddenly there will be an LTS version of JDK17, which will be slightly difficult to accept.

✍Summary

The new features of JDK15 as a whole are not very eye-catching. It mainly determines the functions of the preview features of the previous version, such as text blocks, ZGC, etc., so that we can use them with confidence.

I really can't learn the speed of publishing the version once every six months, but fortunately I have my insistence: you send whatever you want, and I use Java8.

Official account background reply: JDK15, one-click package to get IDEA2020.2.2 + JDK15 installation package (mac + windows)

✔ Recommended reading:

Tags: Java

Posted by regexpert on Mon, 16 May 2022 10:05:40 +0300