About Records preview added in Java JDK 14


Java is fast, and JDK 15 will be released soon! (Scheduled for September 2020) However, to be honest, I wasn't able to catch up with JDK14, which was just before that. After a lot of research, I found out that the concept of Records was released as a preview version on JDK14, so I wrote a little article.

What is Records

Below. https://openjdk.java.net/jeps/359 Record is now provided for data storage, The class with this will result in an immutable object. Specifically, it has the following form.

record Point(int x, int y) { }

The above results in the following classes.

class Point extends Record {
  private final int x;
  private final int y;

  public Point(int x, int y) {
    this.x = x;
    this.y = y;

  public int x() {
    return x;
  public int y() {
    return y;

  public int hashCode() { ... }
  public boolean equals() { ... }
  public String toString() { ... }

As mentioned above, the field variable is immutable except for the constructor, A class that implements the ʻequals hashCode`` toString` method is generated.

It seems that the value of the argument can be checked as follows.

record Point(int x, int y) {
  public Range {
    //When only values less than x can be set for y
    if (x > y) throw IllegalArgumentException();
    //It will set the value without explicitly setting it as shown below.
    // this.x = x;
    // this.y = y;


It's still a preview version, so it's unclear what will happen in the future, I thought that it would be intuitively understandable by using it in DTO or by using Record. Personally, the expression "named tuple" was the best. For those who want to know more deeply, the following article was very easy to understand. https://www.infoq.com/jp/articles/java-14-feature-spotlight/

Java is evolving steadily ... I will catch up properly. Thank you for reading!

Recommended Posts

About Records preview added in Java JDK 14
A story about the JDK in the Java 11 era
About abstract classes in java
List of members added in Java 9
About file copy processing in Java
List of types added in Java 9
About returning a reference in a Java Getter
[Creating] A memorandum about coding in Java
[Java] Set AdoptOpen JDK in STS (Eclipse)
Continued Talk about writing Java in Emacs @ 2018
About Java interface
About the confusion seen in startup Java servers
About the idea of anonymous classes in Java
Java installation jdk
[Java] About arrays
Try scraping about 30 lines in Java (CSV output)
Changes in Java 11
About var used in Java (Local Variable Type)
Rock-paper-scissors in Java
Something about java
Where about java
About Java features
About Java threads
[Java] About interface
About Java class
About Java arrays
About java inheritance
About interface, java interface
Pi in Java
FizzBuzz in Java
About List [Java]
About java var
About Java literals
About Java commands
I wrote about Java downcast in an easy-to-understand manner
[Java] Things to note about type inference extended in Java 10
Think about the JAVA = JAVAscript problem (needed in the future)
About full-width ⇔ half-width conversion of character strings in Java
About Java log output
About Java functional interface
[java] sort in list
Read JSON in Java
Make Blackjack in Java
Java, about 2D arrays
Constraint programming in Java
About class division (Java)
Put java8 in centos7
Q & A about JDK
NVL-ish guy in Java
Combine arrays in Java
About [Java] [StreamAPI] allMatch ()
The Records that are preview released in JDK 14 are nice, so let's try it with JShell
About Java StringBuilder class
"Hello World" in Java
Callable Interface in Java
About eval in Ruby
[Java] About Singleton Class
Comments in Java source
Azure functions in java
[Java] About anonymous classes
About method splitting (Java)