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).
Here, for the time being, the contents are extracted by focusing on the language specifications of Java SE 13 (JSR 388).
There are few changes in JDK13 this time.
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