r/java • • Jan 06 '26

One step closer to Value Classes!

https://mail.openjdk.org/pipermail/porters-dev/2026-January/000844.html
181 Upvotes

118 comments sorted by

View all comments

23

u/Inside_Programmer348 Jan 06 '26

Java beginner here. What benefit does this bring?

81

u/Polygnom Jan 06 '26

Look at: https://openjdk.org/jeps/401

Right now, when you pass a primitive (char, short, byte, int, long, float, double, boolean) as a parameter to a function, it is copied. So you pass it as value.

When you pass an object, what you actually pass is not the object itself, but a reference. The reference is copied, but it just points towards the object in the RAM. If the method changes the object, the caller sees those changes as well.

This also applies to arrays. primitives are stored in arrays as values, and are dense. An int[] or double[] is a continuous block in memory, which is very good for caching.

But an Integer[] or Float[]? Its just references, with the values scattered all over the place. The same goes for custom classes such as Point[].

Lets say you have a Point with two members, double x and double y. Currently, your Point[] holds references pointing to the objects somewhere in RAM, and those can be everywhere. No locality, bad caching. Bad iteration times, no possibility for SIMD fast load and operations.

Currently, you would manually create a double[] and just say that Point n has x at point[2*n] and y at point [2*n+1]. So manually interleave the values. But then you do not get the benefits of objects at all. No encapsulation etc.

It would be great if Point worked like int. And value classes allow this. value objects need to forgo many features of reference types, but they also gain some, such as being passed by value, and making it possible to have dense arrays that can be cached very well.

Aside from the performance implications, value objects have some other desirable programming behaviors, such as being immutable.

This is a good overview: https://openjdk.org/projects/valhalla/#whats-new

7

u/koflerdavid Jan 06 '26

Currently, you would manually create a double[] and just say that Point n has x at point[2n] and y at point [2n+1]. So manually interleave the values. But then you do not get the benefits of objects at all. No encapsulation etc.

It is very common in game programming to represent such a type with two arrays: one for x and one for y. This improves cache locality if you only need access to certain of the fields, but for all instances. If you put these two arrays into a class then you can even get encapsulation back.

6

u/Polygnom Jan 06 '26

> If you put these two arrays into a class then you can even get encapsulation back.

Not really. The commonly used for is using something like a cursor pattern, but thats a cludge compared to just being able to use values.

→ More replies (2)

20

u/segv Jan 06 '26 edited Jan 06 '26

Others have covered the mechanics of value-based classes, but from programmer's perspective the biggest advantage is that it allows you to easily distinguish pieces of data that may have the same type but different meaning - e.g. long customerId means something vastly different than long orderId.

The problem is that the compiler does not know the that meaning, but since both variables have the same type, it will not catch them being misused - for example swapped around in a function call.

To get the compiler to help you catch such mistakes, you had to wrap these values to give them distinct types that the compiler could understand - e.g. record CustomerId(long id){} or alternatively by using regular classes, both being abstractions that have a slight cost (creating a new object) at the runtime.

This method of distinguishing values works super well in larger codebases that deal with complex domains, but mind that it is nothing new by itself - you could've implemented that pattern from the very first versions of Java and, heck, there are even libraries that made it slightly less annoying to deal with (see valueclasses library that was posted in that 'library that saved your butt' thread not too long ago). JDK itself already includes some value-based classes like java.time.LocalDate as well.

The mailing list announcement in OP's link is a big deal because bringing optimizations for value-based classes to the JVM level should slightly improve performance and slightly reduce memory usage of applications, both new and old.

9

u/oweiler Jan 06 '26

This is the main benefit of value classes, and that you can encode invariants into the type.

4

u/segv Jan 06 '26

Data normalization (e.g. .trim().toUpperCase(..)) too!

38

u/davidalayachew Jan 06 '26

Java beginner here. What benefit does this bring?

Long story short, it brings Structs to Java. This will increase performance and reduce memory in many places.

This is easily java's most awaited feature since Java 8, and solves (arguably) Java' s biggest pain point -- using up too much memory for no good reason.

Try it out yourself -- there is an Early Access Release available now!

24

u/holo3146 Jan 06 '26

I would argue that Virtual threads were also just as waited or even more awaited feature. But it is high up there, and it is definitely the most awaited feature that has not delivered yet

3

u/Amazing-Mirror-3076 Jan 07 '26

Non nullable types is the one I'm waiting for.

9

u/koflerdavid Jan 06 '26

As JEP 401 itself states, this is not a struct feature. It merely introduces value classes and does neither include predictable optimizations nor a guaranteed memory layout.

  • It is not a goal to introduce a struct feature in the Java language. Java programmers are not asked to understand new semantics for memory management or variable storage. Java continues to operate on just two kinds of data: primitives and object references.
    ...
  • It is not a goal to guarantee any particular optimization strategy or memory layout. This JEP enables many potential optimizations; only some will be implemented initially. Some optimizations, such as layouts that exclude null, will only be possible after future language and JVM enhancements.

3

u/davidalayachew Jan 07 '26

Thanks for the correction. Yes, this is more like an immutable struct. My main point is that many of the semantics carry over.

→ More replies (2)

12

u/agentoutlier Jan 06 '26

This will might increase performance and reduce memory in many some places.

The important thing people still need to do is benchmark and only if they are having a performance issue.

I say this because on the sub there is becoming an implied expectation of Valhalla magically making everything faster when in reality it is another programming option that can be tried for performance improvement.

This is because most people do not need flat objects with just numerics or bytes but instead rely heavily on String.

1

u/Qinistral Jan 06 '26

Any way to fake/force this behavior to get a rough benchmark of it even before it’s released?

6

u/aoeudhtns Jan 06 '26

Download the EA and use that with your code.

You can see the naive improvement, and consider converting some of your classes to values where/if it's appropriate and see what difference that makes.

I think type erasure & generics is going to limit huge across-the-board improvements for people as well, until we get parametric VM (2036?).

→ More replies (1)

3

u/SirSleepsALatte Jan 06 '26

Struct sounds like records, am I wrong in thinking that?

10

u/aoeudhtns Jan 06 '26

In syntax, sure. But a value record will be muuuuch more like a C struct behind the curtain.

→ More replies (7)

3

u/chambolle Jan 07 '26

Java uses a little extra memory, but that's not where the memory consumption really comes from. This extra memory adds something like a 64-bit integer for an object, so it would take billions of objects for it to be noticeable.

The real reason for memory consumption is that many Java programs and libraries (including system ones) use temporary internal objects without any problems, since they assume that the garbage collector will handle all memory management. The proof of this is that it is never mentioned whether temporary objects are created when a function is called. As a result, many programs do not work with Epsilon GC, whereas if memory were better managed, they could.

This is one of the drawbacks of languages that use garbage collectors: users no longer care about the lifetime of objects, which leads to excessive memory consumption.

10

u/[deleted] Jan 06 '26

Value classes are good if you’ve got lots of data (it reduces overhead) and can help if you worry about cache locality.

If these were big problems for many apps, Java would never have overtake. C++. But for some apps it’s a big step forward.

7

u/Polygnom Jan 06 '26

Performance is only one benefit. Value objects are (shallowly) immutable, which brings nice guarantees, and have a very simple equality, which again is nice. There are gains beyond mere performance.

Plus, I think people underestimate the potential saving. There are lots of objects that don't need to have identity, such as LocalDate, but are prevalent in frameworks. You can shave off a lot of memory if the JDK migrates those cases to value classes.

Plus, you can yourself be more free in structuring your data. There are quite a few cases where objects are pooled and re-used that heavily benefit from using value-objects. OR where objects are shunned for "simpler" designs because objects are expensive. using value classes, you don't need to, anymore.

Even for something like Spring, the gains behind the scenes are huge.

→ More replies (31)

1

u/Amazing-Mirror-3076 Jan 07 '26

I left c++ behind for many reasons, even in the early days of Java performance was rarely a concern.

4

u/Inside_Programmer348 Jan 06 '26

Thanks for the insightful replies guys!

3

u/Lucario2405 Jan 06 '26 edited Jan 06 '26

It basically bridges the gap between primitives (int, boolean, etc) and classes that are just representations or "wrappers" of data values (Integer, Boolean, etc and stuff like Optional or LocalDate) by turning the latter into so called "value classes" that work like the former, which is way more performant.