What javac does
Text blocks
By the time it reaches the class file, a text block is an ordinary String constant.
A text block (Java 15) is a String literal with a different syntax. javac strips the incidental indentation, normalises line endings and processes escapes at compile time, so the class file holds an ordinary String constant and the method is just ldc and areturn.
The source
// min-release: 15
// A text block: by the time it reaches the class file, it is an ordinary String constant.
public class TextBlock {
static String json() {
return """
{
"name": "carafe",
"kind": "jvm"
}
""";
}
}Compiled by javac 25 with -g and --release 17 to 25. Where releases produce different bytecode, each version is shown.
What to look for
- The constant uses
\nline endings whatever the source file used. - The closing
"""lines up with the content, so all of the indentation shared by the lines was removed.
TextBlock.class
static String json()
javac --release 17 to 25 (the same bytecode for every release)