Make writing a habit together! This is the 9th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

Java8 was released on March 18, 2014. As of April 6, 2022, the current latest release is Java18. Versions 17, 11, and 8 are the long Term Support (LTS) versions currently supported. This article takes you through the features of each version of Java from 8 onwards. Sit back and go! Want to have a crush on an article, click here to interview the kill | please discuss Java8-18 of the new features introduced (3)

New features in Java 13

Text Blocks

Text block minimization A Java syntax required to represent multi-line strings that can be used in place of any string that is conventionally added in double quotes. Before text blocks, if we had to print a multi-line string, we would need to use delimiters, concatenations, etc.

For example, the following code gives the entire string in one line

System.out.print("Hey There " + "What's up?? " + "How was your vacation?? "+") ");Copy the code

Output:

Hey There What's up?? How was your vacation?? ;)
Copy the code

To print them on the next line, we must change the code above to the code given below,

System.out.print("Hey There \n" + "What's up?? \n" + "How was your vacation?? \n" + ";) ");Copy the code

Using text blocks, we can rewrite the code above into the code shown below,

System.out.print(""" Hey There What's up?? How was your vacation?? ;) "" ");Copy the code

Text blocks can also be used instead of standard strings. For example, the two strings shown below have the same meaning:

//Text Block printMsg(""" Print This!! "" "); // String in Double Quotes printMsg("Print this!" );Copy the code

Switch Expressions (JEP 354)

We first saw Switch Expressions in JDK 12. Switch Expressions of 13 builds on the previous version by adding a new yield statement.

Using yield, we can now effectively return a value from the switch expression:

@Test
@SuppressWarnings("preview")
public void whenSwitchingOnOperationSquareMe_thenWillReturnSquare() {
    var me = 4;
    var operation = "squareMe";
    var result = switch (operation) {
        case "doubleMe" -> {
            yield me * 2;
        }
        case "squareMe" -> {
            yield me * me;
        }
        default -> me;
    };
​
    assertEquals(16, result);
}
Copy the code

As we can see, it is now easy to implement the strategy mode using the new switch.

Text Blocks

Text blocks of multi-line strings, such as embedded JSON, XML, HTML, etc.

To embed JSON in our code, we declare it as String literal:

String JSON_STRING 
  = "{\r\n" + ""name" : "namestr",\r\n" + ""website" : "https://www.%s.com/"\r\n" + "}";
Copy the code

Now let’s write the same JSON using a String text block:

String TEXT_BLOCK_JSON = """
{
    "name" : "namestr",
    "website" : "https://www.%s.com/"
}
""";
Copy the code

Obviously, there is no need to escape double quotes or add carriage returns. By using text blocks, embedded JSON is easier to write, read, and maintain.

In addition, all String functions are available:

@Test
public void whenTextBlocks_thenStringOperationsWorkSame() {        
    assertThat(TEXT_BLOCK_JSON.contains("namestr")).isTrue();
    assertThat(TEXT_BLOCK_JSON.indexOf("www")).isGreaterThan(0);
    assertThat(TEXT_BLOCK_JSON.length()).isGreaterThan(0);
}
Copy the code

In addition, java.lang.String now has three new methods for manipulating text blocks:

  • StripIndent () – Mimics the compiler to remove attached whitespace
  • TranslateEscapes ()- Translate escaped sequences such as “t” to “t”
  • Formatted ()- Formatted works in the same way as String: : : format, but is used for text blocks

Let’s take a quick look at the String: : : formatting example:

assertThat(TEXT_BLOCK_JSON.formatted("baeldung").contains("www.baeldung.com")).isTrue();
assertThat(String.format(JSON_STRING,"baeldung").contains("www.baeldung.com")).isTrue();
Copy the code

There are other new features, including but not limited to:

  • Dynamic CDS Archives (JEP 350)
  • ZGC: Uncommit Unused Memory (JEP 351)
  • Reimplement the Legacy Socket API (JEP 353)

To continue, the following will continue to talk about the new features of each version, stay tuned!