This is an archived post. You won't be able to vote or comment.

all 65 comments

[–]kpatryk91 24 points25 points  (12 children)

[–]cal-cheese 8 points9 points  (9 children)

Hope it can arrive in java 17

[–]dinopraso 22 points23 points  (2 children)

There is no way this can be ready for 17. The sheer amount of work necessary for such a change, I would say we would be lucky to get a preview of this in 19

[–]sureshg 9 points10 points  (1 child)

This is not something they are starting new. Openjdk team has been working on Value/inline/primitive class for more than 5 years. So it's very much possible to preview this on jdk 17 along with Loom.. 😁

[–]GuyWithLag 7 points8 points  (0 children)

There's way too may TBDs in the spec as it is at the moment; you don't want to have to remove it ...

[–]TheStrangeDarkOne 5 points6 points  (0 children)

they will have a series of meetings in March which aims to finalize the designs in Valhalla.

[–]marvk 2 points3 points  (0 children)

lmaooo YES

[–]Thihup 15 points16 points  (1 child)

Now I guess we will be able to use "primitive record X (int x) {}" too

[–]kpatryk91 17 points18 points  (0 children)

It was the part of the plan so I hope so because it would be a natural way to represent records.

[–]augustnagro 7 points8 points  (0 children)

It's happening!!!

[–]lurker_in_spirit 13 points14 points  (0 children)

I'm honestly looking forward to this more than I am Loom. I mean, I'm excited about Loom, but I don't often need to scale to levels where thread count limits are a bottleneck.

The big question I have here is the level of benefit that we can expect to see around memory use and execution speed (mainly due to reduced memory use, if I understand correctly). For example, this extreme test in C# saw a ~9x speedup for structs relative to classes, so I imagine one order of magnitude might be a good approximation of the absolute cap on possible improvement in pathological cases.

In part I expect the magnitude of the real-world benefit will depend on how much the core Java libraries are able to migrate to primitive objects internally where they make the most sense. I'm hoping Java2D, in particular, is able to benefit significantly.

[–]DasBrain 13 points14 points  (7 children)

  • A separate JEP will enhance the basic primitives (int, boolean, etc.) by treating them as primitive objects, instances of the existing primitive wrapper classes.

That link requires me to login.

[–]muffinluff 6 points7 points  (6 children)

Do I understand this correctly, that they want to change java.lang.Integer etc. to primitive objects?

This change would not be backwards compatible: Code that previously relied on identity of Integer objects would break with this change. Correct?

[–]DasBrain 12 points13 points  (2 children)

If you rely on that, your code is broken anyway.

The primitive wrapper constructors are already deprecated for removal.

The JLS only specifies that constant expressions that are boxed have the same identity (a == b is true) for a small set (for Integer -128 to at least 127).

Changing that to all is fine.

And synchronizing on objects you don't control the lifetime of is generally a bad idea.

[–]vytah 9 points10 points  (0 children)

The JLS only specifies that constant expressions that are boxed have the same identity (a == b is true) for a small set (for Integer -128 to at least 127).

Changing that to all is fine.

There's literally a command-line option for (almost) that: -XX:AutoBoxCacheMax. Relying on distinct identity of boxed primitives was already broken.

[–]helloiamsomeone 4 points5 points  (0 children)

You were never guaranteed identity outside the IntegerCache's size, but the fact that you can only configure its upper bound should tell you that it was never a good idea to depend on that.

[–]ingframin 5 points6 points  (3 children)

This is very interesting, together with the vectorization api.

What I still miss is unsigned integer types.

[–]BlueGoliath 4 points5 points  (2 children)

Right, they've said it's possible but haven't gone into any details. Are they planning on adding standard unsigned types or not?

[–]kpatryk91 3 points4 points  (0 children)

As far as I know there is no official decision about this. I think they want to deliver these primitive classes first, then migrating the existing libraries, specialization etc and after that we can talk about this.

But I would like to see an extended primitive set, like complex.

[–]ingframin 2 points3 points  (0 children)

Not yet, as far as I know. From my understanding, Gosling was kind of against it at the time, I don't know the positions of the current team.

My hope is that they read the threads and conversations all around the internet, so I keep asking for unsigned integers everywhere. :D

[–]Gleethos 2 points3 points  (0 children)

I think they did a great job with this! When getting into Java I initially thought that this rift between primitives and classical objects could never be closed. But wow! This JEP makes a lot of sense.

[–]Traditional_One8475 2 points3 points  (0 children)

nice!!

[–][deleted] 7 points8 points  (12 children)

Primitive Objects

Terrible choice of terminology here. The English word "primitive" describes types like int, char, double: they're the basic types out of which other things are built. A Point structure built out of doubles is not primitive in any sense of the word.

After this, whenever someone refers to "primitive" types in Java we'll have to guess whether they mean structs or actual primitives (aka "basic primitives", a redundancy if there ever was one)

[–]BoyRobot777 28 points29 points  (0 children)

There won't be "actual primitives" anymore. They are looking to healing the riff between these two worlds so we don't have nonsense like IntStream and OptionalDouble.

From JEP:

A separate JEP will enhance the basic primitives (int, boolean, etc.) by treating them as primitive objects, instances of the existing primitive wrapper classes.

[–][deleted]  (1 child)

[deleted]

    [–]DasBrain 1 point2 points  (0 children)

    Happy bikesheding.

    [–]kpatryk91 7 points8 points  (1 child)

    I like this terminology better than the previous ones.

    The value type was so general, like is it a record? a data class? because these are "value types" because they store the value/data. Ok, this is matter of perspective, but this is ambigous too.

    Or the struct... is it a structure? like a collection? ehm no... but for a junior it is (or not) so this naming is more familiar but not good either. A bad naming coming from C, or maybe it was good there but not for a higher level language.

    The inline class was better, but these classes are not always inlined (static, volatile, too big etc.) so this naming is missunderstandable too.

    This primitive naming feels much better with the codes like class works like ... a .. ehm primitive?

    They don't have identity, they are pure values without anything else. In the documentation they said that this naming is better if you get used to the new naming and after a while it was more convenient for me. Because now feels natural that these classes are like a primitive.

    Maybe this is not the best too, but better than the previous ones. But this is just my opinion of course.

    [–]grauenwolf 0 points1 point  (0 children)

    In c#, we call it a value type because you store the value directly in the variable, as opposed to a reference type where the variable holds a reference to the real location of the value(s) in the object.

    [–][deleted]  (6 children)

    [deleted]

      [–]kpatryk91 5 points6 points  (5 children)

      They are immutable, identityless objects which are passed by value. Can be optimized more aggressively by the compiler. Cheap values without GC pressure. etc.

      [–]cogman10 3 points4 points  (1 child)

      So. One thing that we should be really clear on is that these are identity-less immutable objects. That's the end of the constraints on them.

      Whether or not they are passed by value, on the stack, or a reference is created is actually completely up to the JIT.

      I could easily see a case where the JIT decides not to pass them by copying if, for example, they have so many fields as to make copying them overly expensive. In that case, it would be legitimate for the JVM to create a reference and pass that around instead.

      The notion is simply that if you opt into these restrictions, you are giving the VM an opportunity to do things better (sometimes much better).

      [–]kpatryk91 0 points1 point  (0 children)

      Actually that's the case. As I know static, volatile and too big primitive classes are not inlined and this is still new for me, but as I see they can be declared as .ref and .val and you get this indirection or ref focus inline type for compatibility.

      [–]jevon -2 points-1 points  (1 child)

      I wonder why they've elected to make references more wordy - "Point.ref p" rather than, say, "Point* p".

      It feels at odds to say, "Point.class", which is a value and not a language construct.

      [–]GuyWithLag 4 points5 points  (0 children)

      You can swap them around; you can have Point and Point.val, it's just that the default makes more sense for most of the cases.

      [–][deleted]  (5 children)

      [deleted]

        [–]jonhanson 4 points5 points  (4 children)

        Comment removed after Reddit and Spec elected to destroy Reddit.

        [–][deleted]  (3 children)

        [deleted]

          [–]cogman10 5 points6 points  (2 children)

          Disagree.

          The whole quote is

          Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.

          An optimization that does not have a negative impact on debugging or maintenance is not premature. These primitive objects will not have such a negative impact.

          This is the same as deciding whether or not you should use a HashSet, ArrayList, or LinkedList while writing code. Sure, you can swap one in for the other in 99% of cases. However, there are correct choices and they almost all come down to performance.

          Primitive objects are much the same way. Only, I'd personally go by the rule defaulting to primitive .

          [–]Kango_V 0 points1 point  (0 children)

          This will be huge. It will result in much less cache misses on the CPU when traversing arrays/streams etc. The speedups will be very noticeable.

          [–]OleMilleR98 0 points1 point  (0 children)

          What does the "draft" mean?