Interview to kill | Please talk about the new features introduced by Java8-18 (4)

Get into the habit of writing together! This is the 9th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Java8 was released on March 18, 2014, and as of April 6, 2022, the latest release is Java18. Versions 17, 11 and 8 are the currently supported Long Term Support (LTS) releases. This article leads you to review the features of each version starting from Java 8. Sit on the bench and go! If you want to read the previous article, click here for an interview and anti-kill | Please talk about the new features introduced by Java8-18 (3)

What's New in Java 13

Text Blocks

A text block minimizes a Java syntax required to represent multi-line strings, which can be used in place of any string that is conventionally enclosed in double quotes. Before the text block, if we had to print a multi-line string, we would need to use delimiters, concatenation, etc.

For example, the following code will give the complete string in one line

System.out.print("Hey There "
+ "What's up?? "
+ "How was your vacation?? "
+ ";)");
复制代码

output:

Hey There What's up?? How was your vacation?? ;)
复制代码

In order to print them on the next line, we have to modify the above code to the code given below,

System.out.print("Hey There \n"
+ "What's up?? \n"
+ "How was your vacation?? \n"
+ ";)");
复制代码

Using text blocks, we can rewrite the above code to the code given below,

System.out.print("""
Hey There
What's up??
How was your vacation??
;)
""");
复制代码

Text blocks can also be used in place 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!");
复制代码

Switch Expressions (JEP 354)

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

Using yield, we can now effectively return values ​​from switch expressions:

@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);
}
复制代码

As we can see, the strategy mode is now easily implemented using the new toggle.

Text Blocks

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

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

String JSON_STRING 
  = "{\r\n" + ""name" : "namestr",\r\n" + ""website" : "https://www.%s.com/"\r\n" + "}";
复制代码

Now let's write the same JSON using String text blocks:

String TEXT_BLOCK_JSON = """
{
    "name" : "namestr",
    "website" : "https://www.%s.com/"
}
""";
复制代码

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

Additionally, 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);
}
复制代码

Additionally, java.lang.String now has three new methods for manipulating blocks of text:

  • stripIndent() - mimics the compiler's removal of incidental whitespace
  • translateEscapes() - translates escape sequences like "t" to "t"
  • Formatted() - works the same as String:: : : format, but for blocks of text

Let's take a quick look at an example of String: : : formatting:

assertThat(TEXT_BLOCK_JSON.formatted("baeldung").contains("www.baeldung.com")).isTrue();
assertThat(String.format(JSON_STRING,"baeldung").contains("www.baeldung.com")).isTrue();
复制代码

Of course 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 be continued, let's continue to talk about the new features of each version, so stay tuned!

Guess you like

Origin juejin.im/post/7084371995835498526