Sam Howard Sam is a developer at IDRsolutions who specialises in font rendering and conversion. He's also enjoyed working with SVG, Java 3D, Java FX and Swing.

Text Blocks preview in Java 13

2 min read

This month we are focusing on Java 13 new features you should know about. We’ve already written about what is new in Java 13 and the improved garbage collection.Now, we’re talking about Text Blocks.

A preview of things to come?

Before we get started, however, we should mention that Text Blocks are a preview language feature in JDK 13. That means two things:

  1. If they are received well, they could stick around as specified here. If they don’t, they could see major changes or complete removal when Java 14 comes.
  2. They are disabled by default. To enable, either change your language level to include preview in your IDE, or add --enable-preview if you’re using the command line.

What’s the problem?

Until now, including multi-line text in your code has required a lot of '\n' or similar. Take, for example, this variable for storing JDeli’s help message:

final String info = "JDeli - Java Decoding and Encoding Library for Images\n"
+ '\n'
+ "Features:\n"
+ "\tEncoders (Write to Image File formats from Java)\n"
+ "BMP, JPEG, JPEG2000, PDF, PNG\n"
+ '\n'
+ "\tDecoder (Read into Java from image File formats)\n"
+ "\tBMP, DICOM, GIF, JPEG, JPEG2000\n"
+ "\tPNG, PSD, SGI, TiFF, WebP, WMF\n"
+ '\n'
+ "Homepage: https://www.idrsolutions.com/jdeli\n"
+ "License: https://www.idrsolutions.com/jdeli/license\n"
+ "Javadoc: https://files.idrsolutions.com/maven/site/jdeli/apidocs/\n"
+ '\n'
+ "--compresspng file.png "
+ '\n'
+ "--convert outputFormat inputDir outputDir"
+ '\n'
+ "--help";

Not the most readable code, I’m sure you’d agree, but about as good as you can get with standard strings.

How can Text Blocks help?

Let’s have a look at the same code using a Text Block:

final String info = """
JDeli - Java Decoding and Encoding Library for Images

Features:
\tEncoders (Write to Image File formats from Java)
BMP, JPEG, JPEG2000, PDF, PNG

\tDecoder (Read into Java from image File formats)
\tBMP, DICOM, GIF, JPEG, JPEG2000
\tPNG, PSD, SGI, TiFF, WebP, WMF

Homepage: https://www.idrsolutions.com/jdeli
License: https://www.idrsolutions.com/jdeli/license
Javadoc: https://files.idrsolutions.com/maven/site/jdeli/apidocs/

--compresspng file.png
--convert outputFormat inputDir outputDir
--help""";

Much better! Because we’ve cleared all of the concatenation and newlines out of the way, we can actually see our text. The string version makes it difficult to tell where lines begin and end – note how the first 14 lines include a newline at the end of the line, only for the last few to go with newlines between the lines.

But we can still go one better by replacing escaped spacing with real spacing:

final String info = """
JDeli - Java Decoding and Encoding Library for Images

Features:
Encoders (Write to Image File formats from Java)
BMP, JPEG, JPEG2000, PDF, PNG

Decoder (Read into Java from image File formats)
BMP, DICOM, GIF, JPEG, JPEG2000
PNG, PSD, SGI, TiFF, WebP, WMF

Homepage: https://www.idrsolutions.com/jdeli
License: https://www.idrsolutions.com/jdeli/license
Javadoc: https://files.idrsolutions.com/maven/site/jdeli/apidocs/

--compresspng file.png
--convert outputFormat inputDir outputDir
--help""";

As a result, we can see the text exactly as it will appear when printed out. This lets us easily see that some of the indentation is missing.

Where does the block begin?

By definition, text blocks need to handle white space differently to normal strings. It would look very odd if text blocks required you to remove all indentation, but equally you probably don’t want that extra white space to appear in the final string.

Because of this, the Java compiler strips out an equal amount of white space from each line such that at least one line starts with a non-blank character. (Escaped blank characters like '\t' can also indicate the end of white space to strip, as can a closing """.)

An additional bonus of text blocks is that " can be used unescaped, which should prove particularly useful if you need to store code snippets in Strings.

Sam Howard Sam is a developer at IDRsolutions who specialises in font rendering and conversion. He's also enjoyed working with SVG, Java 3D, Java FX and Swing.

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2019. All rights reserved.