Primitive classes in javac
https://github.com/openjdk/valhalla/pull/2691/changes
Javac is being taught about primitive classes as a preview feature. While marked as added in Java 28, I'm guessing we won't have a way to use it considering that there's no JEP even.
From reading the code:
primitiveclasses are implicitlyvalueclasses (JEP 401) (immutable, can beabstractand then optionallysealed...).- Implicitly
staticwhen nested likerecord. - Uniquely, the class name starts (or should start) with a lowercase, like primitive types.
- Interfaces and enums cannot be
primitive. - Can be generic and can implement interfaces like anything else*.
- Unsurprisingly, all the examples are for numeric types (like complex numbers and points).
I assume that they are non-null by default because they are not reference types. I would be cautious about extrapolating from the older Valhalla models that talked about primitive classes, they might not be the same "primitive class".
*except for generic enums :(
58
Upvotes
4
u/davidalayachew 2d ago
Sorry, I'm a bit lost.
!and?suffixes --String!vsString?.Where do Primitive Classes fall into this? Are they an alias of 1 of the 3 above? Is it an entirely new concept?
Bonus points if we can get a link to actual Project Valhalla documentation.
8
u/rzwitserloot 2d ago
This suggests you misunderstand a few things, but maybe this was just a poor choice of words.
That statement is like saying: "I assume this circle will not have corners".
Of course it won't. If it had them, it wouldn't be a circle.
primitive classes aren't references. 'null' is a reference, and only a reference. it is not a value.
Hence, the entire concept doesn't apply to them. It is impossible for the JVM to have an expression of <some primitive type> be
nullfor the same reasonint i = null;isn't just a compiler error, it is nonsense.The way java-the-language (and also java-the-vm) works, there must be a default value for everything. i.e. if I write:
Then x needs to have a value already. Even if I try to make
xfinal, I could in theory end up readingxbefore assigning it in a way the compiler cannot catch.The lang spec is clear enough about this: All numeric primitives are
0(charis a numeric primitive;'\0'if you prefer), booleans arefalse, references (which would be everything else) isnull. The same 'its gotta have a default' occurs when making arrays. When you writenew String[5]each and every 'slot' in that array needs to be set to a default value, and the spec is clear as to what that is: it'snull. You can't change it.At the vm level the same thing happens. Conveniently, every default value that the java lang spec defines is represented at the VM level with all zero bits, so 'an array is made; each slot needs to be assigned the default value' boils down to clearing out every bit in the memory space of that array, which is a lot faster than writing the machine code equivalent of a for loop.
For the exact same reason, when primitive types were originally being discussed, the notion 'the default value is all zero bits because that way we don't have to make massive changes to how the JVM works' was more or less baked in as table stakes.
That means if you do this:
and elsewhere:
Then the
psarray will contain 2 points; each point hasx = 0,y = 0, andname = null.ps[0] = null;is a compiler error for the same reasonarrayOfInts[0] = null;would be: null is represented with all zero bits, but putting all zero bits in the memory space that representsps[0]is identical to the notion of anew point(x = 0, y = 0, name = null).That's what 'it is not a reference!' means here: Normally when e.g. you send a string from one method to another, what you actually 'send' is a reference. A pointer. And pointers can be
null.With primitive types, that is not possible. You simply can't do that. You can't send a reference to an int either, in java. You can only send the actual int value. Hence, sending a point (calling a method with a parameter of type
point, and you provide one; you can't not provide a parameter, of course) means sending the entire contents of it. This is partly why primitive classes are a terrible idea for types with tons of content.pointis great as a concept (if using ints, it fits in 64 bits, marvellous!), but this would be quite daft:Don't do that. Simply invoking a method passing a bridge requires putting all that on the stack and the other side retrieving it all, every time.
47
u/brian_goetz 2d ago •
Everyone ... chill. People here are extrapolating wildly from no information, and some people are saying some crazy things. We do explorations all the time. When there's something to be said about "here's how it will work", it won't be zero-information prognostication from people who are not involved in the project who just happened to see a commit go into an experimental branch.
The most sensible things said on this thread so far is (the wildly-understated) "You certainly read too much into it". (I'm glad to see people are interested, and we'll share information when we have something worth sharing.)
3
u/Jon_Finn 2d ago
Intriguing... if I understand a comment in GitHub correctly (and I may not), a primitive class can't have reference fields, so it's truly shallow. It also looks like equals() has to match == , again if I interpret correctly. Speculating wildly, this might just be to 'allow' == to be used as shorthand for equals(), a topic of endless discussion and confusion! Or it might be for other reasons.
2
u/vegan_antitheist 2d ago
I would hate seeing value types having a name that is lower case. It has no identity but it's not a primitive. You still need to use equals instead of == and everything else is also the same.
4
u/Lizz_may0 2d ago
Primitive like monkeys? With coffee?
1
u/_vertig0 1d ago
This is a surprise to me, the last I heard primitive classes were obliterated completely, even if these are not the exact same it's a little blindsiding seeing them return.
4
u/ZimmiDeluxe 2d ago
let them cook / explore in peace. we'll hear about it when the experiments bear out, but all of this could be deleted tomorrow. that said, while we are wildly speculating, having javac warn when
==is used with upper case value types (that could contain reference types) and not when used with lowercase / primitive value types would remove a lot of confusion around when to use==2
u/vegan_antitheist 2d ago
In the end, everything is numeric in some way. UUID is somehow not flagged to be a value type even though it's just two long values. Maybe they really don't want to it be a value type. 128 bits is rather long anyway, so using a reference is probably ok. It really makes most sense to use value types for anything that is truly a value, i.e. transitively identity-less and immutable.
-1
u/vegan_antitheist 2d ago
"Generic Enums" would make no sense. They are constant and you can implement generic interfaces, so they actually are generic.