What is new in Java 13 (for Developers)
Java JDK 13 was released on September 2019 as part of the new release cycle (a new Java version every 6 months). In this article we will learn what are the new features available for developers.
What’s new in Java 13?
For Java JDK 13, there are some changes in the Switch Expressions (introduced in Java 12) and the String literals JEP (that was withdrawn from JDK 12 and made it to JDK 13).
Below is a list of all features included in Java 13 (from Oracle blog):
- JEP 350 – Dynamic CDS Archives: Extends application class-data sharing to allow the dynamic archiving of classes at the end of Java application execution. The archived classes will include all loaded application classes and library classes that are not present in the default, base-layer CDS archive.
- JEP 351 – ZGC: Uncommit Unused Memory: Enhances the z garbage collector to return unused heap memory to the operating system.
- JEP 353 – Reimplement the Legacy Socket API: Replaces the underlying implementation used by the
java.net.Socket
andjava.net.ServerSocket
APIs with a simpler and more modern implementation that is easy to maintain and debug. The new implementation will be easy to adapt to work with user-mode threads, a.k.a. fibers, currently being explored in Project Loom. - JEP 354 – Switch Expressions (Preview): Extends switch so it can be used as either a statement or an expression, which will simplify everyday coding, and prepare the way for the use of pattern matching (JEP 305) in switch.
- JEP 355 – Text Blocks (Preview): Adds text blocks to the Java language, which will simplify the task of writing Java programs by making it easy to express strings that span several lines of source code, while avoiding escape sequences in common cases; enhance the readability of strings in Java programs that denote code written in non-Java languages; as well as support migration from string literals by stipulating that any new construct can express the same set of strings as a string literal, and interpret the same escape sequences, and be manipulated like a string literal.
Let’s take a look at changed made to Switch Expressions and the new Text Blocks.
Before you try these new features, don’t forget to download and install the latest JDK. Also, don’t forget to download the latest version of your favorite IDE (latest Eclipse version or IntelliJ IDEA 2019.3).
Changes in the Switch Expressions (from JEP 354 - Preview Feature)
Let’s compare the Switch Expressions from before Java 12, Java 12 and 13.
Until Java 12:
private String traditionalSwitch(DAY_OF_WEEK dayOfWeek) {
String result;
switch (dayOfWeek) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
result = "Weekday";
break;
case SATURDAY:
case SUNDAY:
result = "Weekend";
break;
default:
result = "Invalid day!";
}
return result;
}
break
statement (valid in Java 12, but not in Java 13):
private String switchJava12(DAY_OF_WEEK dayOfWeek) {
return switch (dayOfWeek) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
break "Weekday";
case SATURDAY:
case SUNDAY:
break "Weekend";
};
}
Java 13 uses yield
to return a value from switch instead of break
. This change was made to avoid confusion with the break
statement.
private String switchJava13(DAY_OF_WEEK dayOfWeek) {
return switch (dayOfWeek) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
yield "Weekday";
case SATURDAY:
case SUNDAY:
yield "Weekend";
};
}
We can also use the case value ->
syntax (introduced in Java 12 and still valid in Java 13)
private String enhancedSwitchCase(DAY_OF_WEEK dayOfWeek) {
return switch (dayOfWeek) {
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> "Weekday";
case SATURDAY, SUNDAY -> "Weekend";
};
}
Text Blocks - String Literals (from JEP 355 - Preview Feature)
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.
Below is a snippet comparing how we used to declare multiple line string and the new feature in Java 13:
private static void textBlocks() {
String beforeQuery = "update products\n" +
" set quantityInStock = ?\n" +
" ,modifiedDate = ?\n" +
" ,modifiedBy = ?\n" +
"where productCode = ?\n";
String updateQuery = """
update products
set quantityInStock = ?
,modifiedDate = ?
,modifiedBy = ?
where productCode = ?
""";
System.out.print(updateQuery);
}
This is a feature already available in JavaScript that I really enjoy using and I’m really glad that this is available in Java as well!
Conclusion
These are very two simple features, which makes easier for us, developers, to keep ourselves up to date with latest Java features. Now let’s wait for Java 14 (March 2020) with more new features!
View the full source code on GitHub.
References:
Happy Coding!