java 13

created onJuly 16, 2022

general availability on 2019-09-17

jeps

new features

jep state summary /remark
jep 351: zgc, uncommit unused memory (Experimental) experimental zgc does not currently uncommit and return memory to the operating system, even when that memory has been unused for a long time. enhance zgc to return unused heap memory to the operating system.
jep 354: switch expressions (Second Preview) second preview extended switch statement that can be used as either a statement or an expression. both forms can use either a “traditional” or “simplified” scoping and control flow behavior. see comments on ‘JEP 325: Switch Expressions (Preview)'
jep 355: text blocks (Preview) 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.

jdk internal

jep summary /remark
jep 350: dynamic cds archives extend 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 353: reimplement the legacy socket api replace the underlying implementation used by the java.net.socket and java.net.serversocket apis with a simpler and more modern implementation that is easy to maintain and debug.

deprecated

none

removed

none

some feature details

JEP 354: Switch Expressions (Second Preview)

seems that nothing changed here in comparison to jep 325: switch expressions (preview)

JEP 355: Text Blocks (Preview)

For an intro of this feature, I quote from the description of JEP 355:

" a text block is a new kind of literal in the java language. it may be used to denote a string anywhere that a string literal could appear, but offers greater expressiveness and less accidental complexity.

the opening delimiter is a sequence of three double quote characters (""") followed by zero or more white spaces followed by a line terminator. the content begins at the first character after the line terminator of the opening delimiter.

the closing delimiter is a sequence of three double quote characters. the content ends at the last character before the first double quote of the closing delimiter.

the content may include double quote characters directly, unlike the characters in a string literal. the use of \" in a text block is permitted, but not necessary or recommended. fat delimiters (""") were chosen so that " characters could appear unescaped, and also to visually distinguish a text block from a string literal.

the content may include line terminators directly, unlike the characters in a string literal. the use of \n in a text block is permitted, but not necessary or recommended. "
jep 355: text blocks (preview), description

example of a text block:

string text = """ apple banana orange """;

this equivalent to:

string text = "apple\nbanana\norange\n";

and equivalent to the concatenated form:

string text = "apple\n" + "banana\n" + "orange\n;

if a line terminator is not required at the end of the string, then the closing delimiter can be placed on the last line of content:

string text = """ apple banana orange""";

compile-time processing

text blocks are processed in three steps:

  • line terminators in the content are translated to lf (\u000a) to achive consistency across platforms.
  • incidental white space surrounding the content, introduced to match the indentation of java source code, is removed.
  • escape sequences in the content (i.e. \n) are interpreted.

incidental white space vs essential white space

incidental white space is:

  • white space used to format the code of the text block with indentation.
  • trailing white space

those two forms of whitespace are very unlikely meant by the developer to end up in the text block.

essential white space is white space that belongs to the content of the literal in the text block code.

for a description how the compile-time process distinguishes between incidental and essential white space, I quote from the JEP 355, compile-time processing:

" the re-indentation algorithm takes the content of a text block whose line terminators have been normalized to lf. it removes the same amount of white space from each line of content until at least one of the lines has a non-white space character in the leftmost position. the position of the opening """ characters has no effect on the algorithm, but the position of the closing """ characters does have an effect if placed on its own line. "
jep 355: text blocks (preview), compile-time processing

so the first example.

string text = """ apple banana orange """;

defines the text block:

apple banana orange

whereas with (note the indentation of the text relative to the closing delimiter):

string text = """ apple banana orange """;

we get the following text block:

apple banana orange

a few other things worth noting

  • double quotes can be used inside text blocks without escaping, even next to the opening or closing delimiters, though, in the later case, the quotes have to be placed the line after the opening delimiter, respective the line before the closing delimiter.
  • inside text blocks, sequences of three double quotes """ require the escaping of at least one double quote like \""" to avoid mimicking of openening/closing delimiters.
  • a text block supports the same escape sequences as a string literal, such as \n, \t, ', ", and \ .
  • text blocks and string literals may be concatenated interchangeably.

reference

openjdk JDK 13 feature list and schedule

JEP 354: Switch Expressions (Second Preview)
JEP 355: Text Blocks (Preview)