Java中的文本块(或多行字符串)功能是什么?

Java中的文本块(或多行字符串)功能是什么?,java,java-15,java-text-blocks,Java,Java 15,Java Text Blocks,JavaSE13引入了(或多行字符串)特性。它与现有的字符串表示法有什么区别和相似之处?什么是文本块? A是一个多行字符串文字,该功能提供了一种以可预测的方式格式化字符串的干净方法,无需使用大多数转义序列。它以一个“”“(三个双引号)开头和结尾,例如 关于缩进的注释: <html> <head> <title>Hello World</title> </head> <body>

JavaSE13引入了(或多行字符串)特性。它与现有的字符串表示法有什么区别和相似之处?

什么是文本块? A是一个多行字符串文字,该功能提供了一种以可预测的方式格式化字符串的干净方法,无需使用大多数转义序列。它以一个
“”“
(三个双引号)开头和结尾,例如

关于缩进的注释:

<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Java rocks!
    </body>
</html>
public class Main {
    public static void main(String[] args) {
        // Text block without a line break at the end
        String str1 = """
                Hello""";

        // Text block with a line break at the end
        String str2 = """
                Hello
                """;

        // Text block with three whitespace in the beginning and a line break at the end
        String str3 = """
                   World!
                """;
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println("Java rocks!");
    }
}
Hello
Hello

   World!

Java rocks!
javac --enable-preview --release 13 Main.java
java --enable-preview Main
true
Hello World!
Hello World! Java rocks!
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Java
        rocks!
    </body>
</html>
What is the distance between earth and moon?
What is the distance between earth and mercury?
What is the distance between earth and moon?
What is the distance between earth and moon?
编译器将整个文本块向左移动,然后保留指定的间距

String str1 = """
   Hello""";
^^^<-----These three whitespace characters will be retained
输出:

<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Java rocks!
    </body>
</html>
public class Main {
    public static void main(String[] args) {
        // Text block without a line break at the end
        String str1 = """
                Hello""";

        // Text block with a line break at the end
        String str2 = """
                Hello
                """;

        // Text block with three whitespace in the beginning and a line break at the end
        String str3 = """
                   World!
                """;
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println("Java rocks!");
    }
}
Hello
Hello

   World!

Java rocks!
javac --enable-preview --release 13 Main.java
java --enable-preview Main
true
Hello World!
Hello World! Java rocks!
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Java
        rocks!
    </body>
</html>
What is the distance between earth and moon?
What is the distance between earth and mercury?
What is the distance between earth and moon?
What is the distance between earth and moon?
它是否仅作为预览功能可用? 它在Java SE 13和Java SE 14中仍然作为预览功能可用,并已通过Java SE 15标准化。与任何预览功能一样,Java SE 13和14必须使用
--启用预览
选项编译和执行,例如

编译:

<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Java rocks!
    </body>
</html>
public class Main {
    public static void main(String[] args) {
        // Text block without a line break at the end
        String str1 = """
                Hello""";

        // Text block with a line break at the end
        String str2 = """
                Hello
                """;

        // Text block with three whitespace in the beginning and a line break at the end
        String str3 = """
                   World!
                """;
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println("Java rocks!");
    }
}
Hello
Hello

   World!

Java rocks!
javac --enable-preview --release 13 Main.java
java --enable-preview Main
true
Hello World!
Hello World! Java rocks!
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Java
        rocks!
    </body>
</html>
What is the distance between earth and moon?
What is the distance between earth and mercury?
What is the distance between earth and moon?
What is the distance between earth and moon?
执行:

<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Java rocks!
    </body>
</html>
public class Main {
    public static void main(String[] args) {
        // Text block without a line break at the end
        String str1 = """
                Hello""";

        // Text block with a line break at the end
        String str2 = """
                Hello
                """;

        // Text block with three whitespace in the beginning and a line break at the end
        String str3 = """
                   World!
                """;
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println("Java rocks!");
    }
}
Hello
Hello

   World!

Java rocks!
javac --enable-preview --release 13 Main.java
java --enable-preview Main
true
Hello World!
Hello World! Java rocks!
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Java
        rocks!
    </body>
</html>
What is the distance between earth and moon?
What is the distance between earth and mercury?
What is the distance between earth and moon?
What is the distance between earth and moon?
它们是否存储在字符串池中? 是的,它们是。文本块被编译成与传统的
字符串
值相同的类型,即字节码不区分传统的
字符串
值和文本块

输出:

<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Java rocks!
    </body>
</html>
public class Main {
    public static void main(String[] args) {
        // Text block without a line break at the end
        String str1 = """
                Hello""";

        // Text block with a line break at the end
        String str2 = """
                Hello
                """;

        // Text block with three whitespace in the beginning and a line break at the end
        String str3 = """
                   World!
                """;
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println("Java rocks!");
    }
}
Hello
Hello

   World!

Java rocks!
javac --enable-preview --release 13 Main.java
java --enable-preview Main
true
Hello World!
Hello World! Java rocks!
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Java
        rocks!
    </body>
</html>
What is the distance between earth and moon?
What is the distance between earth and mercury?
What is the distance between earth and moon?
What is the distance between earth and moon?
文本块是否可以与另一个字符串连接? 是的,文本块可以连接到传统字符串值或其他文本块,连接方式与连接传统
字符串
值的方式相同。如上所述,字节码不区分传统
字符串
值和文本块

输出:

<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Java rocks!
    </body>
</html>
public class Main {
    public static void main(String[] args) {
        // Text block without a line break at the end
        String str1 = """
                Hello""";

        // Text block with a line break at the end
        String str2 = """
                Hello
                """;

        // Text block with three whitespace in the beginning and a line break at the end
        String str3 = """
                   World!
                """;
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println("Java rocks!");
    }
}
Hello
Hello

   World!

Java rocks!
javac --enable-preview --release 13 Main.java
java --enable-preview Main
true
Hello World!
Hello World! Java rocks!
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Java
        rocks!
    </body>
</html>
What is the distance between earth and moon?
What is the distance between earth and mercury?
What is the distance between earth and moon?
What is the distance between earth and moon?
请注意,我在
str1
中的
Hello
后面加了一个空格,在
javarocks!
中的
str3
前面加了另一个空格

它支持转义序列吗? 是的,转义序列将以传统方式进行评估,例如

public class Main {
    public static void main(String[] args) {
        String text = """
                <html>
                    <head>
                        <title>Hello World</title>
                    </head>
                    <body>
                        Java\n\t\trocks!
                    </body>
                </html>""";

        System.out.println(text);
    }
}
输出:

<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Java rocks!
    </body>
</html>
public class Main {
    public static void main(String[] args) {
        // Text block without a line break at the end
        String str1 = """
                Hello""";

        // Text block with a line break at the end
        String str2 = """
                Hello
                """;

        // Text block with three whitespace in the beginning and a line break at the end
        String str3 = """
                   World!
                """;
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println("Java rocks!");
    }
}
Hello
Hello

   World!

Java rocks!
javac --enable-preview --release 13 Main.java
java --enable-preview Main
true
Hello World!
Hello World! Java rocks!
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        Java
        rocks!
    </body>
</html>
What is the distance between earth and moon?
What is the distance between earth and mercury?
What is the distance between earth and moon?
What is the distance between earth and moon?
(主持人注:如果您对其可接受性有意见,请不要在此处发表评论。相反,请发布链接元问题的答案。此处的元评论可能会被删除。)