Scala (case classes) and C# (structs) have support for data classes. When is Java expected to offer support for this language feature?
Data classes, i.e. Java classes whose sole purpose is to hold data and make it accessible via getters and setters, are among the largest collection points of boilerplate code in many software projects.
You have to create a Java class with private fields. And getter and setter methods for the fields. And additional methods like equals() , hashCode() and toString() . But who says you have to create the Java code by hand?
out. print(new Date()); I know that whatever is in the argument is converted to a string, the end value that is, new Date() returns a reference to a Date object.
Record was introduced in Java SE 14 as a preview feature, which is a feature whose design, implementation, and specification are complete but it is not a permanent addition to the language, which means that the feature may or may not exist in the future versions of the language.
With a bit of reseach, I ended up with Data Classes and Sealed Types for Java by Brian Goetz.
Here is the JEP, Records (Preview), linking to the above.
Summary - It is just an idea/JEP. So, we cannot tell when it would be implemented (or if it would be implemented at all)
The tentative date is March 2020 for the release of JDK 14.
In Nov. 2019, the JEP 359 about Records (data carriers classes) has been confirmed as being part of Java 14 as a preview (meaning, as commented below by Basil Bourque, it needs to be enabled explicitly and it can be removed in the future).
Still: you can start experimenting with it starting with Java 14, March 2020.
As presented here, record Range(int lo, int hi)
would replace
package javax0.geci.tests.record;
import javax0.geci.annotations.Geci;
@Geci("record")
public final class Range {
final int lo;
final int hi;
//<editor-fold id="record">
public Range(final int lo, final int hi) {
this.lo = lo;
this.hi = hi;
}
public int getLo() {
return lo;
}
public int getHi() {
return hi;
}
@Override
public int hashCode() {
return java.util.Objects.hash(lo, hi);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Range that = (Range) o;
return java.util.Objects.equals(that.lo, lo) && java.util.Objects.equals(that.hi, hi);
}
//</editor-fold>
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With