Java 13 — New Language Features

Giorgi Tsiklauri
5 min readSep 4, 2021

--

Java 13 proposed two language update features:

  1. Text Blocks (Preview Feature)
    and
  2. Switch Expressions (Second Preview Feature — update, for the feature proposed in Java 12).

Text Blocks (Preview) (JEP 355)

Purpose and the goal of this enhancement, overall, is to improve working with strings in Java. Namely, to:

  1. Simplify writing and working with multi-line texts;
  2. Remove the need of escaping most of the characters that you had to escape before;
  3. Improve readability of strings, in Java programs, that denote code written in non-Java languages (e.g. HTML, SQL, XML, JSON).

What you usually do (or had been doing up until Java 13), whenever you want to create a text (an instance of java.lang.String), that spans multiple lines, and includes symbols, which are interpreted by compiler as instructions, and need to be escaped?

You need to break the long string and concatenate broken parts on each line.. ah.. (tedious, boilerplate and ugly clutter).. and sure, you also need to escape all those characters that have special meaning to the compiler (escape sequences).

So, to print text like:

text includes multiple lines 
and symbols like " or ' or \

you need to create such a string literal:

String text = "text includes multiple lines " +
"\nand symbols like \" or \' or \\";

Let’s bring another example and see what you have to write in Java source code, when you want to create and print an HTML string, like:

<html>    <head>
</head>
<body>
<p align="center">Hello</p>
</body>

</html>

Well, this is the string literal you have to define in your Java code:

String htmlText = "<html>\n\n" +
"\t<head>\n" +
"\t</head>\n" +
"\t\n" +
"\t<body>\n" +
"\t\t<p align=\"center\">Hello</p>\n" +
"\t</body>\n" +
"\t\n" +
"</html>";

Beautiful, very clear and readable, right?.. yeah, sure..

So, the Text Blocks are what makes working with such strings way more pleasant and comfortable.

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.

If we’ll rewrite text and htmlText with Text Blocks, they will look as:

String text = """
text includes multiple lines\s
and symbols like " or ' or \\
""";

and

String textHtml = """
<html>
<head>
</head>

<body>
<p align="center">Hello</p>
</body>

</html>
""";

respectively.

Now, this is really better.. without irony, this time. String blocks are string literals enclosed in:

"""
triple quotes.
"""

Final notes:

  1. You can still use escape sequences in the text blocks, however, sometimes they’ll be unnecessary (e.g. \"). However, if you want to use \t, go for it, text blocks support them;
  2. Opening triple-quotes must be followed by a line terminator. You have to start your string literal on the next line. Closing triple-quotes, however, can be on the line of ending text;
  3. When you type """ opening triple-quotes, and you then introduce your string literal, right on the next line, incidental and unintentional white spaces, surrounding the text content (you create them, effectively, by visually aligning your text content to your comfortable view), are often unimportant trailing spaces, however, they can be the reason of irritation and even problems. This is a big, compilation-design topic, that takes quite long talk, and I won’t go in it here, but I can tell you how to avoid those trailing spaces: whichever triple-quotes """ are left-most (opening or closing), its vertical border-line will be denoting the beginning of the text, and all the other part of text, will be aligned with respect to that line.
    All other trailing spaces before that border, will be automatically removed by compiler. For example:
  """
<html>
<body>
</body>
</html>
""";

will be printed as:

..<html> <!-- does represent spaces -->
<body>
</body>
</html>

Switch Expressions (Second Preview) (JEP 354)

I have already covered, quite thoroughly, the first Preview (JEP 325) of the new feature — Switch Expressions, introduced in Java 12.

So, JEP 325 has been superseded with JEP 354, that made it to the Java 13.

In order to avoid duplication, I will not repeat what I already wrote — the purpose, goals, concept or the idea of the switch expressions. If you haven’t read my post yet, about this topic, please have a look at it, for the background knowledge.

So, what’s new in Java 13’s switch expressions?

Not really much, but yield.

The thing is, JEP 325 proposed, and in Java 12, we had the overloading of the break statement to return a result value from a switch expression.

This design of switch expression, however, after it got delivered in Java 12, as a preview feature, got feedback, that:

this use of break was confusing.

based on which, JEP 354 was created, as a successor of JEP 325, and it proposed only one change:

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

So, if in Java 12, we would have written:

int month = 1;String name = switch (month) {
case 1 -> {
break "Giorgi";
}
default -> {
break "Other name";
}
};
System.out.println(name);

to return/produce/yield a value from switch expression,

in Java 13, yield is used instead of break, to do the same, as:

int month = 1;String name = switch (month) {
case 1 -> {
yield "Giorgi";
}
default -> {
yield "Other name";
}
};
System.out.println(name);

Note, that this works for older switch labels, as well.

JEP 354 was targeted to JDK 13 in June 2019 as a (second) preview feature.

Finally, feedback on JDK 13 suggested, that switch expressions feature was ready to become final and permanent in JDK 14 with no further changes, and so it became, as JEP 361.

--

--

Giorgi Tsiklauri

Software engineer, architect, lecturer; long while w/ computers and music; interested in software design, computer networks, composition, security and privacy.