Previously, when declaring a multi-line text in Java, for example:
1 2 |
Welcome to Huong Dan Java My name is Khanh |
We will usually declare the following:
1 2 |
String text = "Welcome to Huong Dan Java \n" + "My name is Khanh"; |
Result:
But from Java 13 onwards, you do not need to declare so anymore. Java now supports the text blocks with the ability to declare multiple lines without the need for plus signs (+) to concatenate strings. In the above example, we can now rewrite the following:
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.huongdanjava; public class Example { public static void main(String[] args) { String text = """ Welcome to Huong Dan Java My name is Khanh """; System.out.println(text); } } |
You just need to declare your multi-line text between two characters (“””).
The same result: