Thursday 14 July 2022

Text blocks in Java

Text block are introduced in Java15 to simplify the way we define multi line strings.

 

Example

String str1 = """
Hi there,
How are you,
text blocks made my life easier.
""";

 

Without text blocks, above snippet should be defined like below.

String str2 = "Hi there,\n"
		+ "How are you,\n"
		+ "text blocks made my life easier.";

Do I need to escape double quotes in Text blocks?

No, not required.

 

Example

String str3 = """
Hi there,
How are you,
"text blocks" made my life easier.
""";

Trailing spaces are ignored in text blocks

Trailing spaces in every line of text blocks are ignored. If you want to preserve a space, use the escape sequence \s. Java compiler preserves any spaces in front of this escape sequence \s.

String str4 = """
Hi there,        
How are you,          \s
text blocks made my life easier.        
""";

String str5 = "Hi there,\nHow are you,           \ntext blocks made my life easier.\n";

Both str4 and str5 represent the same string.

 

Find the below working application.

 

TextBlocksDemo.java

package com.sample.app;

public class TextBlocksDemo {

	public static void main(String[] args) {
		String str1 = """
				Hi there,
				How are you,
				text blocks made my life easier.
				""";

		String str2 = "Hi there,\n" + "How are you,\n" + "text blocks made my life easier.";

		String str3 = """
				Hi there,
				How are you,
				"text blocks" made my life easier.
				""";

		String str4 = """
				Hi there,
				How are you,          \s
				text blocks made my life easier.
				""";

		String str5 = "Hi there,\nHow are you,           \ntext blocks made my life easier.\n";

		System.out.println("str1 : " + str1 + "\n");
		System.out.println("str2 : " + str2 + "\n");
		System.out.println("str3 : " + str3);
		System.out.println("str4.equals(str5) : " + str4.equals(str5));
	}
}

Output

str1 : Hi there,
How are you,
text blocks made my life easier.


str2 : Hi there,
How are you,
text blocks made my life easier.

str3 : Hi there,
How are you,
"text blocks" made my life easier.

str4.equals(str5) : true

Can I use text blocks in swich statement?

Yes, you can use text blocks in switch and case statements.


 

TextBlocksInSwitch.java

package com.sample.app;

public class TextBlocksInSwitch {

	public static void main(String[] args) {
		switch ("""
				Hi there,
				How are you,
				text blocks made my life easier.
				""") {

		case """
				text1
				text2
				""":
			System.out.println("First case matched");
			break;

		case """
				Hi there,
				How are you,
				text blocks made my life easier.
				""":
			System.out.println("Second case matched");
			break;

		case "3":
			System.out.println("Third case matched");
			break;

		}
	}

}

Output

Second case matched

Reference

https://docs.oracle.com/javase/specs/jls/se18/html/jls-3.html#jls-3.10.6

https://docs.oracle.com/javase/specs/jls/se18/html/jls-14.html#jls-14.11








 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment