Boundless tutorial: introduction to Java 14 – Text block

Posted by KCKTechs on Thu, 10 Feb 2022 00:20:07 +0100

In Java, a text block is a multiline string text. This means that we don't need to get into the mess of explicit line terminators, string joins, and separators, which are usually used to write ordinary string text.

Java} text blocks are available in Java 13( JEP 355) and Java 14( JEP 368  )As a preview function. It is planned to become a standard function of Java 15(  JEP 378 ) .

To enable this preview function, we must use the - enable preview and - source 14 (two hyphens) flags.

Text block syntax

  • A text block contains multiple lines of text and uses three double quote characters ("" ") as its start and end delimiters.
  • The first three double quote characters are always followed by a line terminator.
  • We cannot include delimiters and text blocks on a line. The separator must be on its own line. Content can only start on the next line.
  • If the text content contains single or double quotation marks, it does not need to be escaped.
String dbSchema = 	"""
			CREATE TABLE 'TEST'.'EMPLOYEE'
			(
			  'ID' INT NOT NULL DEFAULT 0 ,
			  'FIRST_NAME' VARCHAR(100) NOT NULL ,
			  'LAST_NAME' VARCHAR(100) NULL ,
			  'STAT_CD' TINYINT NOT NULL DEFAULT 0 
			);
					""";

Although the text block above looks very simple, there are still a lot of things happening behind the scenes. Let's explore.

Similar string

  • The instance generated from the text block is of type Java Lang. string, which has the same characteristics as the traditional double quoted string. This includes object representation and insertion string pools.
  • We can use text blocks as method parameters of type String.
  • Text blocks can be used anywhere string text can be used. For example, we can use it for string concatenation.
String string = "Hello";
String textBlock = """
					World""";

String joinedString =  string + textBlock;

System.out.println(joinedString);

Program output.

Hello
World

Text block indent

A text block retains the indentation of its contents. Let's refer to the first example.

String dbSchema = 	"""
			CREATE TABLE 'TEST'.'EMPLOYEE'
			(
			  'ID' INT NOT NULL DEFAULT 0 ,
			  'FIRST_NAME' VARCHAR(100) NOT NULL ,
			  'LAST_NAME' VARCHAR(100) NULL ,
			  'STAT_CD' TINYINT NOT NULL DEFAULT 0 
			);
					""";

System.out.println(dbSchema);

Program output:

|CREATE TABLE 'TEST'.'EMPLOYEE'
|(
|  'ID' INT NOT NULL DEFAULT 0 ,
|  'FIRST_NAME' VARCHAR(100) NOT NULL ,
|  'LAST_NAME' VARCHAR(100) NULL ,
|  'STAT_CD' TINYINT NOT NULL DEFAULT 0
|);

Here, we have two indentation types:

The first indent is the word "CREATE" from the beginning of the line to all lines. It may increase or decrease depending on various factors, such as formatting plug-ins or developer choices.

The second indentation starts from the character '(' ') to' ID '. In most cases, the indentation time is 4 or 8 spaces.

Add some indents

Suppose we want to add 2 tab indents to the left for all rows in the above example. To do this, we can carefully position the closing three quotation marks to move the two tabs precisely to the left. It should start at exactly the same position as the retracted push rod.

String dbSchema = 	"""
			CREATE TABLE 'TEST'.'EMPLOYEE'
			(
			  	'ID' INT NOT NULL DEFAULT 0 ,
			  	'FIRST_NAME' VARCHAR(100) NOT NULL ,
			  	'LAST_NAME' VARCHAR(100) NULL ,
			  	'STAT_CD' TINYINT NOT NULL DEFAULT 0 
			);
	""";

System.out.println(dbSchema);					

Program output:

|		CREATE TABLE 'TEST'.'EMPLOYEE'
|		(
|  			'ID' INT NOT NULL DEFAULT 0 ,
|  			'FIRST_NAME' VARCHAR(100) NOT NULL ,
|  			'LAST_NAME' VARCHAR(100) NULL ,
|  			'STAT_CD' TINYINT NOT NULL DEFAULT 0
|		);

Also, note that the Java compiler also removes trailing spaces on each line in the text block.

Line terminator

Different platforms have different line terminators. Java does not participate in platform detection, but standardizes all line terminators in text blocks to \ n.

If a platform line terminator is required, you can use String::replaceAll("\n", System.lineSeparator()).

String string = "Hello";
String textBlock = """
					World""";

String joinedString =  string + textBlock;

joinedString = joinedString.replaceAll("\n", System.lineSeparator());

System.out.println(joinedString);

Escape sequence

Most of the time, we just want to write the content to multiple lines of the program, but in fact, they are just a string content. In this case, we can use the line feed character or the code break character, that is, a single backslash '\'. It prohibits the inclusion of implicit line breaks.

String dbSchema = """
	CREATE TABLE 'TEST'.'EMPLOYEE'\
	(\
	'ID' INT NOT NULL DEFAULT 0 ,\
	'FIRST_NAME' VARCHAR(100) NOT NULL ,
	'LAST_NAME' VARCHAR(100) NULL ,\
	'STAT_CD' TINYINT NOT NULL DEFAULT 0 \
	);
	""";

System.out.println(dbSchema);					

Program output:

|CREATE TABLE 'TEST'.'EMPLOYEE'('ID' INT NOT NULL DEFAULT 0 ,'FIRST_NAME' VARCHAR(100) NOT NULL ,
'LAST_NAME' VARCHAR(100) NULL ,'STAT_CD' TINYINT NOT NULL DEFAULT 0 );

If for some reason we don't want to remove the indentation, we can use '\ s'(ASCII character 32, space) to escape the sequence. Using it at the end of any line ensures that the line has all space characters until '\ s' is encountered.

String dbSchema = 	"""
	CREATE TABLE 'TEST'.'EMPLOYEE'          \s
	(                                       \s
	  'ID' INT NOT NULL DEFAULT 0 ,         \s
	   'FIRST_NAME' VARCHAR(100) NOT NULL , \s
	  'LAST_NAME' VARCHAR(100) NULL ,       \s
	  'STAT_CD' TINYINT NOT NULL DEFAULT 0  \s
	);                                      \s
	""";

System.out.println(dbSchema.replaceAll("\s", "."));

Program output. In this program output, I replace all space characters with dots'. ' Let you know its effect.

CREATE.TABLE.'TEST'.'EMPLOYEE'...........
(........................................
..'ID'.INT.NOT.NULL.DEFAULT.0.,..........
...'FIRST_NAME'.VARCHAR(100).NOT.NULL.,..
..'LAST_NAME'.VARCHAR(100).NULL.,........
..'STAT_CD'.TINYINT.NOT.NULL.DEFAULT.0...
);.......................................


Source: https://www.learnfk.com/article-java14-java-text-blocks

Topics: Java