Java SE 13 (JSR388) has been released so I tried it

As announced by Oracle at Oracle Code One 2019, Java 13 has been released.

[Breaking news] Java 13 is here. New features such as improved ZGC and realization of Switch type. Oracle Code One 2019-Publickey JDK 13 Documentation - Home

The JDK is also distributed to the Opne JDK (as a matter of course).

JDK 13

Here, for the time being, the contents are extracted by focusing on the language specifications of Java SE 13 (JSR 388).

Major changes

There are few changes in JDK13 this time.

Java SE 13 (JSR 388)

JEP 354: Switch Expressions (Preview)

Extend switch so it can be used as either a statement or an expression, and so that both forms can use either traditional case ... : labels (with fall through) or new case ... -> labels (with no fall through), with a further new statement for yielding a value from a switch expression. These changes will simplify everyday coding, and prepare the way for the use of pattern matching (JEP 305) in switch. This is a preview language feature in JDK 13.

Simply put, the switch can be extended into statements and used as expressions and statements. Even in Java 12, JEP 325: Switch Expressions (Preview) had a Preview function, but it has been reexamined and is now a Preview function. ..

Looking at the reexamined content JEP 354: Switch Expressions (Preview), it is described as follows.

To yield a value from a switch expression, the break with value statement is dropped in favor of a yield statement.

This is to generate a value by declaring the return value when describing in {} block as a new yield statement when returning the value by branching the switch statement.

Below is an implementation sample

import java.time.LocalDate;
import java.time.DayOfWeek;

public class Tester_01 {
    public static void main(String... args) {
        LocalDate today = LocalDate.now();
        System.out.println(java13(today.getDayOfWeek()));
    }
        private static int java13(DayOfWeek today) {
        int numLetters =  switch (today) {
            case MONDAY, FRIDAY, SUNDAY -> 6;
            case TUESDAY                -> 7;
            default                     -> {
                int k = today.toString().length();
                yield k;
            }
        };
        return numLetters;
    }
}

Since it is a Preview version, the --enable-preview option is required at runtime as shown below.

workspace> javac --enable-preview --release 13 .\Tester_01.java
Caution:.\Tester_01.java uses the preview language feature.
Caution:Detail is,-Xlint:Please recompile with the preview option.
PS C:workspace> java --enable-preview  Tester_01

JEP 355: Text Blocks (Preview)

Add text blocks to the Java language. A text block is a multi-line string literal that avoids the need for most escape sequences, automatically formats the string in a predictable way, and gives the developer control over format when desired. This is a preview language feature in JDK 13.

Previously, when writing a multi-line string, the string ("one-dimensional" string literals) enclosed in " was written using the + operator, but" By enclosing it in ""(" two-dimensional "block of text), it is no longer necessary to describe the + operator.

Traditional notation

ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
Object obj = engine.eval("function hello() {\n" +
                         "    print('\"Hello, world\"');\n" +
                         "}\n" +
                         "\n" +
                         "hello();\n");

Notation by JEP355

ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
Object obj = engine.eval("""
                         function hello() {
                             print('"Hello, world"');
                         }
                         
                         hello();
                         """);

It seems to be useful when implementing SQL using annotations.

We will investigate the API separately. If you have any questions or something is wrong, please make an edit request.

Recommended Posts

Java SE 13 (JSR388) has been released so I tried it
Java9 was included, so I tried jshell.
Macchinetta has been released
I took Oracle Java Bronze SE 7/8 now, so I will write it as an article
Apple Silicon compatible version of Docker Desktop (Preview) has been released to the public, so I will try it
parquet-tools gives java.lang.ExceptionInInitializerError, so I made it work with java8
I took Java SE8 Gold.
I tried Drools (Java, InputStream)
I tried using Java REPL
I tried metaprogramming in Java
I tried UDP communication with Java
I tried the Java framework "Quarkus"
I tried using Java8 Stream API
I tried using JWT in Java
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
I tried using Java memo LocalDate
I tried using GoogleHttpClient of Java
Macchinetta has been released
Resilience4j document Japanese translation (unofficial) has been released
Java SE 13 (JSR388) has been released so I tried it
Introductory hands-on for beginners of Spring 5 & Spring Boot 2 has been released
Kotlin has no ternary operator (conditional operator)
The JacocoReportBase.setClassDirectories (FileCollection) method has been deprecated.
Java had a QR code creation library called QRGen that wrapped ZXing nicely, so I tried it.
Java12 came out, so I tried the switch expression for the time being
I tried Cassandra's Object Mapper for Java
I tried to summarize Java lambda expressions
Java SE 8 Silver (Java SE 8 Programmer I) Pass Note
I tried using OpenCV with Java + Tomcat
I found MyBatis useful, so I wrote it.
Lite Rx API Hands-on I tried it
I tried Google's entrance exam (unofficial) [java]
Since it was accepted by Java SE 8 Silver, I will introduce various things
I had a hard time doing Java multithreading from scratch, so organize it
I can't remember the text file input / output in Java, so I summarized it.
I stumbled on the Java version in Android Studio, so I will summarize it