What is new in Java 13 (for Developers)

10 Jan 2020
4 mins read

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):

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!