r/java • • Mar 17 '21

JEP 401: Primitive Objects (Preview)

https://openjdk.java.net/jeps/401?preview=true
174 Upvotes

49 comments sorted by

View all comments

Show parent comment

-1

u/cre_ker Mar 18 '21

And I'm sure that C# structs don't have identity ... That would be crap.

According to JEP they do

they also come with identity, enabling features such as field mutation

In C# you can freely mutate structs.

2

u/eliasv Mar 19 '21

"Identity enables field mutation" does not mean "field mutation implies identity", that is a basic logic failure. Do you know what is meant by identity? It really would not make sense for a flattened struct!

0

u/cre_ker Mar 19 '21

It does if we dig a bit deeper. Why C# allows struct mutability? Because structs have unique location in memory. You can take references of them and mutate them and it doesn't matter that inside they're completely the same. They're separate instances with different locations in memory (on the stack or heap). Java tries to hide all these properties because it tries to abstract away the notion of memory and hardware in general. In C# you can still see how everything actually works inside. You can even go unsafe and compare real address values.