I love the fact that I don’t have to deal with the distinction between objects and primitive values in languages such as C# and Kotlin ‘cause everything is an object including primitive values. This JEP is amazing, a great move for the next version of Java.
Well you have to because in C# value types are objects but they have value semantics and mutable. Every time you assign them to something you make a copy. This JEP essentially proposes the same thing but objects are immutable. I’m not sure about that. Seems much more limiting and, depending on implementation, might require tons of copying like with any immutability simply because you have to allocate another instance on every mutation.
Not really, since the primitive classes don't have unique identity the JVM can safely perform copy elision. Such as p = new Point(2, p.y()) will be executed just as p.x = 2. If the inlining is unreliable, they can make the constructor of primitive classes ForceInline anyway.
The plus side is that you don't introduce a whole new kind of "things" into the language, which greatly increases the complexity of the language itself.
It is introducing whole new thing. These primitive objects behave very different from everything else. Essentially these are immutable objects. Not introducing whole new thing would actually be if this JEP did the exact same thing that C# structs do. At least they have identity like everything else and mutable.
perform copy elision
If JVM/javac are smart enough. I'm not sure they will be. It would require pretty complex analysis.
Immutable objects aren't really a "whole new thing", it's effectively just an implicit final on fields. And I'm sure that C# structs don't have identity ... That would be crap.
"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!
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.
37
u/Hall_of_Famer Mar 18 '21
I love the fact that I don’t have to deal with the distinction between objects and primitive values in languages such as C# and Kotlin ‘cause everything is an object including primitive values. This JEP is amazing, a great move for the next version of Java.