What is the syntax of three double quotation marks in Java? Java 15 refreshes your understanding!


What is the syntax of three double quotation marks in Java?

This is java 15 new, refresh your understanding!

1、 Preface

stayJava 15At the time of its launch,Text BlocksFormal transfer, I call it “text block” well, the stack leader also made a brief introduction, did not see can clickhereTake a look.

A text block is a multiline string that avoids most escape symbols, automatically formats the string in a predictable way, and allows developers to control the format when needed.

The text block was first prepared to be added in JDK 12, but it was eventually withdrawn. Then it was added as a preview feature in JDK 13, and then it was previewed again in JDK 14. In JDK 15, the text block finally became regular, and no further changes were made.

2、 Examples

Let’s take a look at this HTML language example

Do not use text blocks::

String html1 = "\n" +
              "    \n" +
              "Hi, Java technology stack"+
              "Welcome to follow and share more dry goods"+
              "    \n" +

Use text blocks:

String html2 = """
                      Hi, Java technology stack
                      Welcome to pay attention and share more dry goods

Text blocks need to be wrapped in three double quotation marks. As anyone familiar with Python knows, three double quotation marks represent multi line comments. Unexpectedly, Java is used as a multi line string

Let’s look at the bytecode

See? After the text block is compiled, the new line character will be added automatically, which is consistent with the original splicing compilation result, and there is no need to splice strings any more.

3、 Detailed introduction

In fact, the role of text block is far more than line breaks. Here is a detailed introduction of stack length.

1. Line terminators

As mentioned above, automatically adding line breaks during compilation can be used to enhance the readability of strings of code written in non java languages. For example, we often need to splice HTML, XML, SQL language code, etc., and the code is very ugly.

line 2
line 3

It is equivalent to:

"line 1\nline 2\nline 3\n"


"line 1\n" +
"line 2\n" +
"line 3\n"

If you don’t need to wrap the last line, you can write this:

line 1
line 2
line 3"""

You can also define an empty text block:

String empty = """

An empty string also requires two lines of code, so this method is not recommended.

2. Unexpected white space

Continue to look at the code above:

Note that this line does not have a row of spaces in front of it, which means that it will not be compiled in. It is only used to indent and format for easy reading, so I call it space arrangement.

If “” occupies a single row at the end, it is left aligned, otherwise it is left aligned by the leftmost column.

Idea is really powerful. It supports JDK 15 so well. If you say no activation code, this is not a matter. You can pay attention to the public number Java technology stack, reply: idea, the stack length teaches you to get the official account free.

In addition, Java 15 addsString.stripIndentInstance method, you can directly delete the beginning and end of each line of white space characters.

3. Escape sequences (escape characters)

As shown in the following code:

I added it to the code\s\n\tSuch escape characters, can be used directly, double quotes do not need to escape.

Results the output was as follows

Hi,  	" Java technology stack“

        Welcome to share "more" dry goods

Practical work may appear in the spelling of SQL statements to save!

Do not use text blocks:

String query = "SELECT \"EMP_ID\", \"LAST_NAME\" FROM \"EMPLOYEE_TB\"\n" +
               "WHERE \"CITY\" = 'INDIANAPOLIS'\n" +
               "ORDER BY \"EMP_ID\", \"LAST_NAME\";\n";

Use text blocks:

String query = """
               WHERE "CITY" = 'INDIANAPOLIS'
               ORDER BY "EMP_ID", "LAST_NAME";

Of course, you should pay attention not to cause SQL injection vulnerabilities when splicing SQL. Here is just the introduction of the use.

In addition, Java 15 adds aString.translateEscapesEscape method, you can directly escape the string content.

4. Format parameters

Before Java 15, you need to use theString.formatStatic method is not very convenient, but a new method of directly formatting string is added in Java 15

When the above SQL statement is extended, the parameters are formatted with a placeholder

String query = """
               WHERE "CITY" = '%s' AND author = '%s'
               ORDER BY "EMP_ID", "LAST_NAME";
        System. Out. Println (query. Formatted ("Shenzhen", "stack length"));

Results the output was as follows

Where "city" ='shenzhen 'and author ='stack length'

Using string objects directlyformattedExample method feel more convenient!

4、 Conclusion

It’s much more difficult to interpret text blocks than you think. I didn’t expect that there are so many details in a text block. This paper only makes a partial summary of the text block, which is not the whole content of the text block. Anyway, it can’t be used for the time being. You can simply understand it.

For those who are interested in text blocks and want to know more about the details of text blocks, please refer to the official instructions:


The official introduction text block uses more than 30000 words… It is conceivable that it is difficult to interpret it.

This time the stack long share is here, hope to be useful to you. I think it’s good. I’ll share it with you~

Finally, official account official Java will continue to update, pay attention to Java technology stack public number first push, and also get history Java tutorial in public menu, dry cargo official account.
Recommended by recent hot papers:

1.Java 15 officially released, 14 new features, refresh your cognition!!

2.Finally, I got the IntelliJ idea activation code through the open source project!

3.I use Java 8 to write a piece of logic, colleagues can not understand, you try..

4.The performance of undertow is very good!!

5.“Java Development Manual (Songshan version)” the latest release, download!

Feel good, don’t forget to like + forward!