This is the article on the 23rd day of Yuru Web Study Group @ Sapporo Advent Calendar 2020. This is the first post on Qiita in about a year.
This time, I would like to touch on the new features of ** Java15 ** released on September 15, 2020.
The main changes in Java15 are summarized in the URL below. https://openjdk.java.net/projects/jdk/15/
In this article, we'll cover three things:
--Text block --Record --Pattern matching
A text block that allows you to define string literals that include newlines. It was implemented as a preview function from Java 12, but this time it was released as the official version.
Enclose the text block in three double quotes (" ""
).
String htmlText = """
<html>
<body>
<span>Text Blocks</span>
</body>
</html>
""";
There are surprisingly many opportunities to handle multi-line character strings such as HTML and JSON (Isn't JSON written solid?), So I think it's a very convenient function in terms of improving the readability of the source code.
Main features of text blocks:
--Only line breaks immediately after the start delimiter (characters cannot be written in succession) --Whitespace at the end of each line is removed --Indentation is unified with the shallowest indentation on each line in the text block
Please check the JEP page for other specifications and how the functions were added. JEP 378: Text Blocks
Records allow you to concisely define immutable classes for holding data. It was implemented as a preview version in Java14, and is now the second preview version.
When defining a record, describe it as record class name (specify argument) {}
.
Let's look at a concrete example.
Suppose you define the fields name and age in a data class called Person.
public record Person(String name, int age) {}
With just the above description, the constructor, getter method, etc. are automatically generated at compile time. The amount of code written in the data class is overwhelmingly small! !!
public class Person extends Record {
private final String name;
private final int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
//The getter method name is
public String name() {
return name;
}
public int age() {
return age;
}
// ...Omitted below...
}
If you want to add an argument check in the constructor, you can extend the process as needed.
Records is still a preview version, so if you want to run it in Eclipse, please enable the preview function from the setting screen below.
reference: JEP 395: Records
This does not refer to a regular expression, it means that when you check the type of an object with the instanceof
operator, you do not need to cast the type.
The traditional instanceof
operator had to be cast after type checking as follows:
if (obj instanceof String) {
String s = (String)obj;
System.out.println(s.length());
}
When pattern matching is used, casting after type checking is not required, and it will automatically store up to the point where it is stored in a variable of the specified type.
if (obj instanceof String s) {
System.out.println(s.length());
}
reference: JEP 375: Pattern Matching for instanceof (Second Preview)
A preview version of Java is a feature that is experimentally implemented before officially incorporating a new feature. Basically, it will be officially released by following the steps below.
① Implemented as a preview version ② Implemented as a second preview version ③ Implemented as the official version
The preview feature receives feedback from developers and may eventually be officially released or deprecated.
From the new features of Java15 that were just released the other day, we have picked up and introduced the features that are relatively easy to understand and introduce.
In the field, I often hear people say that "Java has a lot of description and is troublesome compared to other languages" (tears), but looking at the recent releases, the readability of the source code and the improvement of development efficiency have improved. You can see that the improvements for this are being adopted more and more.
I would like to actively adopt new writing styles so that I can convey the goodness of Java to others.
-New features of Java 14 and Java 15 taught by Java professionals -Java 15 New Features Summary
Recommended Posts