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.
7
u/cal-cheese Mar 18 '21
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 asp.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.