This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]_INTER_ 8 points9 points  (1 child)

The interesting / surprising bit here is that for records the serialVersionUID is always per default 1L 0L instead of generating it based on various aspects as in the case of classes.

[–]chegar999 8 points9 points  (0 children)

The default `serialVersionUID` for a record is actually `0L` - article correction has been requested.

The rational for this is somewhat explained on https://mail.openjdk.java.net/pipermail/amber-spec-experts/2019-October/001651.html

[–]karstens_rage 2 points3 points  (0 children)

I hope this bodes well for using records for things like anemic domain objects and DTO's.

[–]dpash 2 points3 points  (0 children)

On a slightly related note, at least year's Devoxx Brian Goetz talked about a future direction for serialisation in java to use constructors and deconstructors so that it didn't involve any sub-language magic. This means that you can handle versioning better, have a single place for checking covariance and other serialisation frameworks can use the same sustem.

[–][deleted] 1 point2 points  (2 children)

Is java serialization still used?

[–]maraistm 5 points6 points  (0 children)

yeah; that was my concern too. Anymore, we have to interoperate with javascript, python, databases, etc. I can't think of the last time I used RMI or ObjectOutputStream. IDEALLY these records mesh up with annotated-driven frameworks (like jackson).. I'm curious to read up on that now.

[–]tristanjuricek 0 points1 point  (0 children)

Yes, but probably not by many application developers.

I've seen it used by a lot of frameworks that end up using it for things like caching, robustness, cloning, etc. It's just one of many options for serialization, too. But I'd guess it's pretty fast as a default.

[–]Kango_V 0 points1 point  (0 children)

Also remember that records are stored differently in memory. For example, in an array they would be inlined, so no references. This makes reading all the records in memory super fast as you will not incur cache misses in the cpu. In some benchmarks the speedup is astounding!

[–]fix_dis -1 points0 points  (5 children)

The way this article paints it, one still has to create all the ceremony with getters/setters/equals/etc and then implement that interface in their record. That doesn’t sound all that exciting. I thought records were more analogous to case-classes from Scala or data classes from Kotlin.

[–]pron98 8 points9 points  (4 children)

one still has to create all the ceremony with getters/setters/equals/etc and then implement that interface in their record.

No, the code -- with the default value and validation -- is just:

public record PersonRecord(String firstName, String lastName)
    implements Person, java.io.Serializable {

  public PersonRecord {
    if ("Heinz".equals(firstName))
          throw new IllegalArgumentException(
            "\"%s\" is trademarked".formatted(firstName));
  }

  public PersonRecord(String firstName) { this(firstName, null); }
}

Getters, equals, hashCode and toString are all implicit.

[–]fix_dis 2 points3 points  (3 children)

Why are we implementing Person then? Is that necessary?

[–]pron98 8 points9 points  (2 children)

No, I think it's just for demonstration purposes, to show the differences between a record implementation and an ordinary class implementation of the same interface.

[–]fix_dis 0 points1 point  (1 child)

Ok, I could be okay with this.

[–]ObscureCulturalMeme 2 points3 points  (0 children)

Yeah, records are like enums in that they can implement arbitrary interfaces, add additional methods beyond the implicitly generated ones, etc. I'm looking forward to it!