Java 14 new features that could be used to write code

Java14

Java 14 was released on March 17, 2020. Among the functions added in Java14, I would like to introduce the functions that can be used for writing code.

record(preview)

You can easily create a class for holding data.

public record Customer(String name, String age){}

The equivalent of the following class is created.

public class Customer {
    private final String name;

    private final String age;

    public Customer(String name, String age) {
        this.name = name;
        this.age = age;
    }

    public String name() {
        return this.name;
    }

    public String age() {
        return this.age;
    }

    public String hashCode() {...}

    public boolean equals() {...}

    public String toString() {...}
}

Since the method for setting the value is not defined, the value cannot be changed after the instance is created.

Text block

It is now possible to define a character string that includes line breaks. Enclosing a character string with "" instead of "" recognizes it as a text block.

String str1 = "aaaa\n"
    + "bbb";

String str2 = """
   aaa
   bbb\
   """

The above two strings indicate the same thing. If you do not want to start a new line, enter .

Helpful NullPointerExceptions

You can now get a detailed message when a NullPointerException occurs.

String str = null;
str.length(); // NullPointerException

By default, the following exception is output.

Exception in thread "main" java.lang.NullPointerException
	at Test.main(Test.java:5)

In java14, if you execute it with "-XX: + ShowCodeDetailsInExceptionMessages", the following detailed exception will be output.

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "<local1>" is null
	at Test.main(Test.java:5)

Pattern matching instance of (preview)

You can now define variables of that type while checking the type with instanceof.


Object obj = "obj";
if (obj instanceof String str){
    System.out.println(str.length());
}

Recommended Posts

Java 14 new features that could be used to write code
Java 9 new features and sample code
Write code that is difficult to test
java1.8 new features
Java code that cannot be used from Kotlin (for in-house study sessions)
A memo that enabled VS Code + JUnit 5 to be used on Windows 10
Write code that is easy to maintain (Part 1)
How to write code that thinks object-oriented Ruby
Write code that is easy to maintain (Part 4)
Write code that is easy to maintain (Part 3)
Features that are likely to enter Java 10 for now
Basic rules to be aware of to write easy-to-read code
Write a class that can be ordered in Java
How to write good code
The case that @Autowired could not be used in JUnit5
Catch up on new features from Java 7 to Java 9 at once
Things to be aware of when writing code in Java
Whether options can be used due to different Java versions
[Java] Code that is hard to notice but terribly slow
Static analysis tool that can be used on GitHub [Java version]
Summary of ORM "uroboroSQL" that can be used in enterprise Java
The operator that was born to be born, instanceof (Java) ~ How to use the instanceof operator ~
Things to note for newcomers to write readable code during Java development
Introduction to Java that can be understood even with Krillin (Part 1)
Let's write a code that is easy to maintain (Part 2) Name
Java to be involved from today
To be aware of easy-to-read code
How to write easy-to-understand code [Summary 3]
[Android] Convert Android Java code to Kotlin
[RSpec] How to write test code
Correspond to "error that basic authentication does not pass" in the test code "The story that could not be done"
"Let's write versatile code that is easy to use over time" (nth time)
Java file input / output processing that can be used through historical background
Convenient packages that people who have decided to write Java should know
The story that the Servlet could not be loaded in the Java Web application
[Introduction to Java] How to write a Java program
[Java] How to output and write files!
[Java] Flow from source code to execution
[Android] I want to create a ViewPager that can be used for tutorials
About the case that ("b" .. "aa") could not be used in Ruby Range
[CentOS8] Since the javac command could not be used, install the java development environment.
Technology excerpt that can be used for creating EC sites in Java training
View used to execute Java code during debugging in Eclipse is Debug Shell
I didn't know that inner classes could be defined in the [Java] interface