[Java] Java 13 New Features Summary

4 minute read

Java 13 was released on September 17, 2019. Java SE 13 Platform JSR 388

Download

It could be downloaded from the OpenJDK site. https://jdk.java.net/13/

Currently it can be downloaded from the archive site. https://jdk.java.net/archive/

It’s not LTS, so I don’t think you need to download it for any purpose other than verification. SDKMAN! is recommended for installation on Mac or Linux

In addition to Oracle OpenJDK, the following distributions are available for free commercial use.

It’s not LTS, so it doesn’t seem to be released on Amazon Corretto.

You can use the Oracle JDK for development purposes, but you must purchase the Java SE Subscription for commercial use.

JEP

The changes are organized as a JEP. https://openjdk.java.net/projects/jdk/13/

This time around 5 JEPs are included. JEP 350: Dynamic CDS Archives JEP 351: ZGC: Uncommit Unused Memory (Experimental) JEP 353: Reimplement the Legacy Socket API JEP 354: Switch Expressions (Second Preview) JEP 355: Text Blocks (Preview)

JEP 355: Text Blocks (Preview)

A multi-line string literal has been introduced as a preview.

String html = """
              <html>
                  <body>
                      <p>Hello, world</p>
                  </body>
              </html>
              """;

It becomes standard in Java 15 with the addition of space escapes and more. See Java 14 description for detailed specifications. Java 14 New Features Summary-Qiita

JEP 354: Switch Expressions (Second Preview)

Make the Switch statement an expression.

int numLetters = switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> 6;
    case TUESDAY -> 7;
    case THURSDAY, SATURDAY -> 8;
    case WEDNESDAY -> 9;
};

Compared to Java 12, the syntax for returning a value in a switch expression has changed from break to yield. It has been standardized in Java 14. See Java 14 description for detailed specifications. Java 14 New Features Summary-Qiita

JEP 350: Dynamic CDS Archives

Create a CDS archive at runtime by adding -XX:ArchiveClassesAtExit. I think this is helpful for how to use it. Java 13 Dynamic CDS boots faster than expected-Kishida Hatena

In the feeling that I tried, it was faster to start the CDS archive normally than at the time of execution. Self-powered dump from Java 10 boots faster than Dynamic CDS-Kishida Hatena

JEP 351: ZGC: Uncommit Unused Memory (Experimental)

The memory not used by ZGC will be released.

JEP 353: Reimplement the Legacy Socket API

The TCP socket API is reimplemented to support asynchronous processing in Project Loom.

API

Some APIs have also changed.

String

TextBlocks related API has been added. However, as of Java 13, you can always use it by just specifying @Deprecated, but in Java 14, you can use it only with --enable-preview.

formatted

Allow String.format to be used as an instance method.

For example

callQuery(String.format("select * from table where id=%d", id));

Was written

callQuery("select * from table where id=%d".formatted(id));

It is convenient to write like. https://bugs.openjdk.java.net/browse/JDK-8203444

translateEscapes

Treat \n etc. as escape characters. When I look at the code, it is confusing because the escapes are mixed in, but \n in the character string is converted to a line break.


jshell> var s = "test\\ntest\\n"
s ==> "test\\ntest\\n"

jshell> System.out.println(s)
test\ntest\n

jshell> s.translateEscapes()
$18 ==> "test\ntest\n"

jshell> System.out.println(s.translateEscapes())
test
test

https://bugs.openjdk.java.net/browse/JDK-8223780

stripIndent

Remove the indent.

https://bugs.openjdk.java.net/browse/JDK-8223775

In Map.of, get(null) for Map with 1 element is slim

Map.of has been added in JDK9. In JDK9 and JDK10, Map0 is returned when the number of elements is 0, Map1 is returned when the number of elements is 1, and MapN instance is returned when the number of elements is 2 or more.Map.of().get(null) returned null.

C:\Users\naoki>java\jdk\jdk-10.0.1\bin\jshell
| Welcome to JShell --Version 10.0.1
| For an overview, type: /help intro

jshell> Map.of().get(null)
$1 ==> null

jshell> Map.of(1,2).get(null)
$2 ==> null

jshell> Map.of(1,2,3,4).get(null)
| java.lang.NullPointerException thrown
| at ImmutableCollections$MapN.probe (ImmutableCollections.java:779)
| at ImmutableCollections$MapN.get (ImmutableCollections.java:721)
| at (#3:1)

In JDK11, even if the number of elements is 0, MapN is used and Map.of().get(null) is slimy.

C:\Users\naoki>java\jdk\jdk-11.0.1\bin\jshell
| Welcome to JShell --Version 11.0.1
| For an overview, type: /help intro

jshell> Map.of().get(null)
| Exception java.lang.NullPointerException
| at Objects.requireNonNull (Objects.java:221)
| at ImmutableCollections$MapN.get (ImmutableCollections.java:843)
| at (#1:1)

jshell> Map.of(1,2).get(null)
$2 ==> null

jshell> Map.of(1,2,3,4).get(null)
| Exception java.lang.NullPointerException
| at ImmutableCollections$MapN.probe (ImmutableCollections.java:926)| at ImmutableCollections$MapN.get (ImmutableCollections.java:846)
| at (#3:1)

In case of Map.of(1,2).get(null), it returned null from JDK9 to JDK12. From JDK13, Map.of(1,2).get(null) is also lukewarm. Previous versions have also been fixed with JDK 12.0.2 and JDK 11.0.4.

http://mail.openjdk.java.net/pipermail/core-libs-dev/2019-April/059533.html

Here, it’s also interesting to use equals call instead of requireNonNull for null check.

http://cr.openjdk.java.net/~smarks/reviews/8221924/webrev.0/src/java.base/share/classes/java/util/ImmutableCollections.java.patch

Number’s default constructor

http://mail.openjdk.java.net/pipermail/core-libs-dev/2019-May/060258.html

A default constructor has been added to the Number class. However, the implementation looks like this.

public Number() {super();}

This is unnecessary because it is implicitly defined even if you do not define it. It looks like I added it to trick lint

Add Reiwa to Japanese Era

The new era “Reiwa” has started on May 1, 2019. The Java 8 update, Java 11, and Java 12 also supported Reiwa in format and parsing, but it was not officially defined in Japanese Era. Since Java 13, JapaneseEra.REIWA can be used.

In Java 11, the era was undecided, so it was displayed as New Era.

jshell> java.time.chrono.JapaneseDate.now()
$1 ==> Japanese NewEra 2-07-21

In 11.0.3, the era has been decided, so it is displayed as REIWA.

jshell> java.time.chrono.JapaneseDate.of(2020,5,1)
$1 ==> Japanese Reiwa 2-05-01

For this, JapaneseEra.NEWERA and JapaneseEra.REIWA were also defined, but it was package private and could not be used from the code, but from Java 13 it becomes public and anyone can use it. became.

tool

There are some changes to rmic.

Prevent rmic with –preview-feature

http://mail.openjdk.java.net/pipermail/core-libs-dev/2019-April/059683.html

rmic is now deprecated. And then erase

[JDK-8217412] deprecate rmic for removal-Java Bug System

It’s gone in Java 15.