Java Text Block

来自WHY42

Text block

public class Test {
    public static void main(String[] args) {
        String message = """
                Hello world!
                    This line is started with indention     
                Reserved LE spaces    \s
                """;
        System.out.println(message.replaceAll("\s", "~"));
    }
}

Output:

Hello~world!
~~~~This~line~is~started~with~indention
Reserved~LE~spaces~~~~~

Bytecode

javap -v Test
Classfile /Users/riguz/Downloads/Test.class
  Last modified 2023年9月25日; size 610 bytes
  SHA-256 checksum 769a49705a7547a2c19757eced479dd5e734e58805d529795080164f325e946f
  Compiled from "Test.java"
public class Test
  minor version: 0
  major version: 65
  flags: (0x0021) ACC_PUBLIC, ACC_SUPER
  this_class: #31                         // Test
  super_class: #2                         // java/lang/Object
  interfaces: 0, fields: 0, methods: 2, attributes: 1
Constant pool:
   #1 = Methodref          #2.#3          // java/lang/Object."<init>":()V
   #2 = Class              #4             // java/lang/Object
   #3 = NameAndType        #5:#6          // "<init>":()V
   #4 = Utf8               java/lang/Object
   #5 = Utf8               <init>
   #6 = Utf8               ()V
   #7 = String             #8             // Hello world!\n    This line is started with indention\nReserved LE spaces     \n
   #8 = Utf8               Hello world!\n    This line is started with indention\nReserved LE spaces     \n
   #9 = Fieldref           #10.#11        // java/lang/System.out:Ljava/io/PrintStream;
  #10 = Class              #12            // java/lang/System
  #11 = NameAndType        #13:#14        // out:Ljava/io/PrintStream;
  #12 = Utf8               java/lang/System
  #13 = Utf8               out
  #14 = Utf8               Ljava/io/PrintStream;
  #15 = String             #16            //
  #16 = Utf8
  #17 = String             #18            // ~
  #18 = Utf8               ~
  #19 = Methodref          #20.#21        // java/lang/String.replaceAll:(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
  #20 = Class              #22            // java/lang/String
  #21 = NameAndType        #23:#24        // replaceAll:(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
  #22 = Utf8               java/lang/String
  #23 = Utf8               replaceAll
  #24 = Utf8               (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
  #25 = Methodref          #26.#27        // java/io/PrintStream.println:(Ljava/lang/String;)V
  #26 = Class              #28            // java/io/PrintStream
  #27 = NameAndType        #29:#30        // println:(Ljava/lang/String;)V
  #28 = Utf8               java/io/PrintStream
  #29 = Utf8               println
  #30 = Utf8               (Ljava/lang/String;)V
  #31 = Class              #32            // Test
  #32 = Utf8               Test
  #33 = Utf8               Code
  #34 = Utf8               LineNumberTable
  #35 = Utf8               main
  #36 = Utf8               ([Ljava/lang/String;)V
  #37 = Utf8               SourceFile
  #38 = Utf8               Test.java
{
  public Test();
    descriptor: ()V
    flags: (0x0001) ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: (0x0009) ACC_PUBLIC, ACC_STATIC
    Code:
      stack=4, locals=2, args_size=1
         0: ldc           #7                  // String Hello world!\n    This line is started with indention\nReserved LE spaces     \n
         2: astore_1
         3: getstatic     #9                  // Field java/lang/System.out:Ljava/io/PrintStream;
         6: aload_1
         7: ldc           #15                 // String
         9: ldc           #17                 // String ~
        11: invokevirtual #19                 // Method java/lang/String.replaceAll:(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
        14: invokevirtual #25                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
        17: return
      LineNumberTable:
        line 3: 0
        line 8: 3
        line 9: 17
}
SourceFile: "Test.java"