r/java • u/Joram2 • Jun 15 '26
What is the use case for a non-value (identity) record with Valhalla?
With Java classes, non-value (identity) classes are fully mutable, the new Valhalla value classes are (shallow) immutable, that's a big difference; often immutability is impractical, so then identity classes make sense. With records, all records are (shallow) immutable, so this is a non-issue. Secondly, some code uses the legacy synchronization/monitor functionality built-in to identity classes; but that's been strongly discouraged for records, so this seems not much of an issue.
Here is ChatGPT on the difference between a Java value-record and a Java non-value, identity record: https://chatgpt.com/share/6a2b8329-0a0c-83ea-b8b3-fe4e40956616
Is there any use case for Java non-value (identity) records? It seems silly to ask devs to write value in front of every record if that is what devs want almost every time they use records.
4
u/Sheikah45 Jun 15 '26
Value classes cannot self-reference their own type.
So a value record is unable to represent a tree structure like Node(String name, Node child). Since a node object can contain another node object.
Brian gives a good explanation on stack overflow. https://stackoverflow.com/questions/63352151/are-java-records-intended-to-eventually-become-value-types/63365195#63365195
17
u/nekokattt Jun 15 '26
I feel the argument here is more around backwards compatibility. Otherwise you could argue in the same way that putting a bang after every type that can't be null is much more silly than marking just the types that can be null in value.
60
u/brian_goetz Jun 15 '26
There are really two separate questions here:
- What is the use case for identity records
- What should the default be
The title asks the former, but your complaint is really about the latter. Let's take them in turns.
There are use cases for identity records, such as tree nodes:
record TreeNode<T>(TreeNode leftChild, TreeNode rightChild, T value) { }
While this does not describe most records, it would be pretty annoying if you could not express TreeNode as a record.
Which brings us to the question of defaults, and Java has a long history of picking "the wrong defaults", like mutability for fields, and arguably "identity by default" is another candidate for this list, if not for all classes, then certainly for records. But that ship sailed in Java 17 -- even though we knew that Valhalla was coming. We had two choices then: sadly watch it sail out of the harbor, or hold off records until we shipped Valhalla. (One can imagine how popular this choice would have been; Records When?) We could only choose identity semantics then, and now that's what records mean, and changing it would surely break someone's program, even if 99.999% of the time the identity adds no "value".
23
u/repeating_bears Jun 15 '26
I guess the main thing is that records already have identity and changing the default would be a breaking change.
I could imagine some case where you'd want to synchronize on a record but yeah, I agree it seems pretty limited
I get it but I also dislike all of Java's context-sensitive defaults and I don't want another one. If records were value by default, you'd need another modifier e.g. `identity record` to get identity back.
I'm already used to changing my IDE templates to put "final" on every new class, and I will change the record template to add "value" to every record. Doesn't really bother me
1
u/vegan_antitheist Jun 22 '26
Records are about immutability. Value types are about not having identity. You can combine those.
Opting out of identity changes the semantics of == and =. When you assign a reference you just copy the reference. When you assign a value typed value you copy all the data.
A record with lots of fields probably performs better with identity. A record with few fields does better when the JVM can just copy those. You wouldn't want a reference to an int. You often wouldn't want to copy 10 longs from a record. But if they are used in arrays where it might make sense then maybe it's better. The decision will probably be difficult at first and we all need to learn how to pick the right types.
Note that by inligning methods the optimiser can prevent copying a record/passing a reference. Then it just doesn't matter.
2
u/ZimmiDeluxe Jun 15 '26 edited Jun 15 '26
If you load a bunch of rows from your database, the identity hash code likely carries the same semantic weight as the value based one for the subset of rows you care about. If the rows are wide and you want to associate additional data with them for your data transformation, using identity records as keys in an identity hash map might be faster / require less memory than using value records (assuming reified generics) as keys in a regular hash map. Of course, measure first.
2
u/cogman10 Jun 15 '26
I think it's just a miss in the spec which has caused them to need to preserve identity.
IMO, the java devs should have put in a "record classes will be value classes, do not use identity operations on them" much like they did with LocalDate. That didn't happen, I'm sure there are reasons it didn't happen. I think it was just a mistake.
IMO, there's pretty much no reason why all records shouldn't be value classes.
0
u/[deleted] Jun 15 '26
[deleted] — view deleted comment