I touched on the new features of Java 15

Introduction

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.

What's new in Java15

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

Text block

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

Record (second preview version)

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. image.png

reference: JEP 395: Records

Pattern matching (second preview version)

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)

Digression: What is the preview version of Java?

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.

Summary

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.

Reference article

-New features of Java 14 and Java 15 taught by Java professionals -Java 15 New Features Summary

Recommended Posts

I touched on the new features of Java 15
java1.8 new features
I tried the new era in Java
What are the updated features of java 13
Looking back on the basics of Java
Predicted Features of Java
I first touched Java ②
I first touched Java ③
I first touched Java ④
I compared the characteristics of Java and .NET
I first touched Java
[Java] Three features of Java
Try the free version of Progate [Java I]
I tried the new feature profiler of IntelliJ IDEA 2019.2.
I summarized the types and basics of Java exceptions
New features from Java7 to Java8
[Java] I implemented the combination.
I studied the constructor (java)
I tried to summarize the basics of kotlin and java
20190803_Java & k8s on Azure The story of going to the festival
Catch up on new features from Java 7 to Java 9 at once
[Java] I thought about the merits and uses of "interface"
[Java] Delete the elements of List
[Java version] The story of serialization
I read the source of ArrayList I read
Note on the path of request.getRequestDispatcher
I read the source of Integer
Java 9 new features and sample code
Consideration on the 2017 Java Persistence Framework (1)
I read the source of Long
I tried the Java framework "Quarkus"
I read the source of Short
I read the source of Byte
I read the source of String
The origin of Java lambda expressions
I tried using GoogleHttpClient of Java
I made the "Sunshine Ikezaki game" I saw on Twitter in Java.
I tried the input / output type of Java Lambda ~ Map edition ~
I tried to summarize the methods of Java String and StringBuilder
I passed the Oracle Java Bronze, so I summarized the outline of the exam.
I tried to display the calendar on the Eclipse console using Java.
The behavior of JS running on `Java SE 8 Nashorn` suddenly changed
My thoughts on the equals method (Java)
[Java] I participated in ABC-188 of Atcorder.
Display text on top of the image
Get the result of POST in Java
By checking the operation of Java on linux, I was able to understand compilation and hierarchical understanding.
Check the contents of the Java certificate store
Examine the memory usage of Java elements
Memorandum of new graduate SES [Java basics]
Memo: [Java] Check the contents of the directory
Kick ShellScript on the server from Java
Introducing the features of JavaFX SceneBuilder container
Samshin on the value of the hidden field
Install OpenJDK (Java) on the latest Ubuntu
About the new Java release model @ Seki Java (2018/07/20)
part of the syntax of ruby ​​on rails
Compare the elements of an array (Java)
I investigated the internal processing of Retrofit
What wasn't fair use in the diversion of Java APIs on Android
Easily measure the size of Java Objects