HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › Text blocks

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 \n line 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)

5return """
0ldc #7// "{\n \"name\": \"carafe\",\n \"kind\": \"jvm\"\n}\n"
2areturn

Open in the viewer

← instanceof patternsAll topicsVarargs →

By the author of this site

Reading bytecode is half the story; running it is the other half. You execute exactly this kind of code in Build a JVM in Rust: Class files, an interpreter, objects and invokedynamic from scratch, on the real JDK class library.

Read Chapter 1 free

Java and the JVM are trademarks of Oracle; this site is not affiliated with Oracle. Field names follow the Java Virtual Machine Specification; the explanations are our own.

Sister sites: What's in my DB · LLM Anatomy · Distribution Atlas · Notation Decoder · uresk.net

Made by Spencer Uresk · About · Files you open stay on your device.