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

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.

1

u/cre_ker Mar 18 '21

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.

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 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.

-1

u/cre_ker Mar 18 '21

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.

4

u/eliasv Mar 18 '21

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.

-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.