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