New candidate JEP: 401: Primitive Objects (Preview)

Michael Kuhlmann jdk at fiolino.de
Tue Apr 20 13:29:21 UTC 2021


Hi,

sorry to coming back to this topic after more than a month, but I 
thought about it several times and want to share my thoughts. Maybe the 
ideas are long discussed already and nobody wants to read about it any 
more, but I looked in the archives and didn't find anything, so I'll 
give it a try. If it's a stupid idea, just let me know, I can live with 
that.

I'm not a contributor, but I'm using Java for more than twenty years, 
since Java 1.1 precisely. I really love the idea of having primitive 
classes, that would be amazing! I'm just concerned that we won't make 
the best use out of it, especially because of compatibility reasons, and 
I was wondering if this can be achieved with a simple design change.

The problems I'm seeing:
* Primitive classes behave very different from standard object classes, 
but users don't immediately see this. You have to look into the 
definition to know whether an instance variable of SomeType will be 
initialized with null or a default value.
* The suffixes .ref and .val don't fit into our concept of class names, 
they look ugly and can easily be mixed up
* That we have to introduce .rel just for the existing classes is even worse
* Existing classes like Optional will be mostly used in their original 
form. That's unfortunate, not that much for performance reasons but 
rather because such a value should never be null, so it could make most 
use out of this concept.
* There's already the discussion to delay the implementation of typical 
primitive classes. Raffaelo proposed to invent classes Decimal64 and 
Decimal128, but it will not be added before this JEP is going live to 
avoid the need of the ugly compatibility hack.
* We have to treat the seven existing primitive types in a very special way.

People are already used to the idea that normal classes start with an 
uppercase character, but primitives are in lowercase characters. The 
predecessor language Oak even defined string as a primitive type. So why 
not picking up this idea and forcing all future primitive types to start 
with lowercase characters as well?

Java has been very concrete in style guides but very relaxed in 
enforcing them in the past. You can define a class named 'integer' 
without problems. I would see this as a design bug and would rather 
enforce some stricter rules.

So we could make it mandatory to have all primitive class names start 
with a lowercase character, more concrete to a character that can be 
converted to an uppercase character. Instead of creating a twin class 
names 'someClass.ref' what is proposed in the JEP, the reference class 
could be named like the primitive class just starting with the uppercase 
character.

So if you define a primitive class 'someType', you immediately have a 
reference type 'SomeType' defined as well.

Existing classes can be directly converted to primitive classes without 
breaking backwards compatibility. For instance, Optional would be 
declared as a primitive class 'optional', and existing code would still 
refer to the reference type 'Optional'. No special treatment of existing 
classes!

Also Raffaelo can already add Decimal64 and Decimal128 to the JDK. Once 
this JEP is live, he only has to change the 'final' modifier to 
'primitive' and make the 'D' in the class name lowercase. That's it!

This is even more important for libraries who want to stay compatible 
for existing JDK releases and want to make use of this feature only later.

There are some more benefits:
* Primitive types are immediately identifiable by their class name
* No ugly suffixes!
* The naming fits very well into the existing primitives. long, float, 
boolean etc. already match with their wrapper classes Long, Float, 
Boolean. Only int and char have to be defined as aliases, as already 
proposed in the JEP. (I know that these primitives still need special 
handling under the hood, but I assume it won't be bigger than in the 
proposed solution, and for users it doesn't matter.)

So in summary, I think it would improve readability and even more 
compatibility.

Maybe I missed something, but wouldn't this make sense?



On 3/17/21 10:49 PM, mark.reinhold at oracle.com wrote:
> https://openjdk.java.net/jeps/401
> 
>    Summary: Enhance the Java object model with user-declared primitive
>    objects, which are class instances that lack object identity
>    and can be stored and passed directly, without object headers or
>    indirections. This is a preview language and VM feature.
> 
> - Mark
> 


More information about the valhalla-dev mailing list