Text Blocks and Developer Productivity: Good News For Java Developers

July 16, 2020
bloggoodnewsXperti



There are many reasons for Java’s enduring popularity in the technology world. Reason number one is the interest elite software engineers take in introducing new features. Each one of which improves productivity and ease for Java coders. One such feature is the ‘text blocks preview language feature’ introduced in JDK13.

With text blocks, Java developers can format multi-line string literals without using escape sequences.

What Are Text Blocks?

String data, without a doubt, is one of the most used data types by coders. It can store anything from a few characters to multiple lines. This flexibility makes it difficult to read or change any string that contains embedded quotation marks or escape characters. Text blocks offer a solution to this problem. They do this by allowing coders to write a multi-line string inside a multiline block.

JEP 355 and JEP 368

Raw string literals were initially introduced in JDK 12. Raw string literals were added in Java to help developers avoid escapes, which is a very common problem when writing strings. But after top developers identified some issues, the proposal was retrieved. One of these being that it was too lightweight typographically.

In 2019, a more ‘round-up’ concept of raw strings was proposed in JEP 355. The elite software engineers behind it reflected on earlier feedback, improving the formatting and the Readability.

All this while also decreasing the frequency of using escape sequences.

Features Offered by Text Blocks

1. Text Blocks Offer More Readability

The use of multi-line string literals in Java is the biggest highlight of text blocks. This feature is now available in almost all programming languages. It also aims to make inline multi-line strings more readable. For this, text blocks:

  • Excluded the need to concatenate multiple strings together
  • Include newline markers, or
  • Use escape nested quotation marks.

Concatenation operators, escape sequences or any visual clutter required in regular string literals are also not required in text blocks.  As a result, it makes working with strings considerably easier.

2. Opening And Closing Delimiters

As string literals still exist in Java, a unique method was required to separate text blocks and string literals. Top software developers decided to use fat delimiters, i.e. “””. For example, let’s look at the following Java Snippet:

class ABC { 
  public void function() {    
    String txt = """      
      Multiple        
        Lines of          
          Text        
        Is     
      Typed here      
      """;  
  }
}

Notice the three quotation marks that delimit the beginning and ending of the block? The previous alternative in Java looked like this:

class ABC {
  public void function() {
    String txt =
      "Multiplen" +
      "  Lines ofn" +
      "    Textn" +
      "  Isn" +
      "Typed heren";
  }
}

To comprehend the complete text in the block, indentation has been removed. Now the line with the least white space characters is used to figure out how many white space characters should be removed from every line. The compiler takes whitespace indentation into consideration by differentiating incidental whitespaces from essential whitespaces.

See the example below:

class ABC { 
  public void function() {    
    String txt = """      
      Multiple        
        Lines of          
          Text        
        Is     
      Typed here      
      """;  
  }
}

The compiler will see this as:

class ABC {
  public void function() {
    String txt =
      "Multiplen" +
      "  Lines ofn" +
      "    Textn" +
      "  Isn" +
      "Typed heren";
  }
}

3. Use of Escape Sequences

To make things less complicated for Java coders, text blocks handle escape sequences exactly like string literals handled them, allowing coders to use the same escape sequences.

When using escape sequences such as n, f, and r, Java developers can vertically format the string without affecting how line terminators are translated.

Escape sequences like b and t, can be used to horizontally format a string without affecting the incidental white space mentioned in the previous example.

4. Using Quotation Marks Inside Text Blocks

One more feature that top software engineers included for the ease of Java coders is the use of quotation marks inside text blocks.
Quotation marks can be used unless they appear in direct sequence. To not confuse these with the closing delimiter, quotation marks need to be used with an escape, as shown in the JEP 355 example:

String code = """
    String text = """
        A text block inside a text block
        """;
    """

5. New Escape Sequences

To make things more convenient, some additional escape sequences were introduced in JEP 368.

1. <line-terminator> Escape Sequence

Its function is to explicitly suppress the insertion of a new line character. For instance:

String text = """
                Lorem ipsum dolor sit amet, consectetur adipiscing 
                elit, sed do eiusmod tempor incididunt ut labore 
                et dolore magna aliqua.
                """;

2. s – Escape Sequence

This translates to a single space which is not processed until incidental white spaces are stripped. An example from JEP 368 shows its use:

String colors = """
    red  s
    greens
    blue s
    """;

Conclusion

Features like text blocks are created for the sole purpose of creating ease and simplicity for developers. And of course, amateurs and elite software developers in the Java community learn to appreciate them in equal measure. Onsite development companies appreciate features like text blocks too. Because they make the code more readable, and easy to understand. This not only benefits developers but also non-developers. Both come closer to understanding a certain degree of complex code.



author

shaharyar-lalani

Shaharyar Lalani is a developer with a strong interest in business analysis, project management, and UX design. He writes and teaches extensively on themes current in the world of web and app development, especially in Java technology.


Candidate signup

Create a free profile and find your next great opportunity.

JOIN NOW

Employer signup

Sign up and find a perfect match for your team.

HIRE NOW

How it works

Xperti vets skilled professionals with its unique talent-matching process.

LET’S EXPLORE

Join our community

Connect and engage with technology enthusiasts.

CONNECT WITH US