r/programming • u/davidalayachew • May 11 '26
8317277: Java language implementation of value classes and objects by MrSimms · Pull Request #31120 · openjdk/jdk
https://github.com/openjdk/jdk/pull/31120For those unaware, this is the Pull Request for Java's JEP 401: Value Classes and Objects (Preview).
Value Classes have been a LONG AWAITED feature for Java, so this Pull Request is proof that we are one step closer to them going into Preview!
BUT PLEASE REMEMBER -- No commitment has been made to target a release yet!
This is merely a PR RFR, and nothing more. All this is is showing us part of what it takes to bring Value Objects to Preview, as well as announcing that we are one step closer to (hopefully!) go to preview. But again, no idea how far away that may be. It could be JDK 27 (coming this September), it could be later.
Just appreciate the PR for what it is -- a window into the work required to make Value Classes a reality. ~3k commits and ~2k classes changed is just a snapshot of the level of effort here. Shows why this JEP has been given an XL rating lol.
85
u/ZimmiDeluxe May 11 '26 edited May 11 '26
+200.598 -13.384
lgtm
edit: minor whitespace error in line 3456
-7
u/WeirdIndividualGuy May 12 '26
But also, just switch to Kotlin at this point. Fully compatible with Java without the need to convert your whole Java codebase. Just write your new code in Kotlin and carry on in bliss
10
u/Venthe May 12 '26 edited May 12 '26
Kotlin too has its problems, most of which are rooted in the JVM and Java interop (looking at you, type erasure).
But as a language in itself? Night and day. Unfortunately too many Java shops are afraid to try something new.
9
u/yk313 May 12 '26
It's a matter of longterm strategy, not just based on vibes.
JVM languages have come and gone. Kotlin definitely seems to be better poised than others but still it's not a decision you make lightly. Though the language itself barely makes a difference, it doesn't help that people are still running Java 8. Try comparing 25+ with Kotlin and the difference is no longer night and day.
3
u/Venthe May 12 '26
It's a matter of longterm strategy, not just based on vibes. (...) Though the language itself barely makes a difference
Nullable types alone makes it worth it; and that's the just the tip of the features. I've coded in Java my whole career, and while I agree that it is not the language (or framework!) that makes or breaks the codebase, it makes writing with it both faster, more expressive as well as way cleaner given modern constructs + not being burdened with the sytax of Java.
JVM languages have come and gone. Kotlin definitely seems to be better poised than others (...)
This argument might had some basis ten years ago, not so much anymore IMO. Since '17 (namely - Google adopting it for Android) it's a safe bet in my personal opinion. And the language itself is already (almost) half the age of the Java itself at this point.
Try comparing 25+ with Kotlin and the difference is no longer night and day.
In stanard lib / language features, sure, but not in the language constructs. There is no reason to stick to plain Java, since - well - you can use all the plain JVM constructs in kotlin. And the lag is minimal - JDK 25 came out in september, Kotlin supporting 25 bytecode came out in december (Not counting betas and RC's).
it doesn't help that people are still running Java 8.
I'm currently supporting both ends, 8 and 21 - so believe me, I know. :) (Plus that one 1.7; but it's on a life support, not actively developed.)
3
u/lanerdofchristian May 12 '26
There is no reason to stick to plain Java, since - well - you can use all the plain JVM constructs in kotlin.
As someone who likes Kotlin: IMO, using the latest Java constructs in Kotlin is a PITA. There's still a very clear disconnect between the Java and Kotlin worlds that's pretty grating if you ever need to cross it (admittedly not helped by most Java libraries themselves not keeping up with new features very well).
The utter lack of pattern-matching comparable to Java is also a sore point for me.
It's also worth considering that Kotlin is a Jetbrains product, far more so than Java is an Oracle one. While they've been making improvements to their LSP implementation (and third-party ones exist), for better or worse if you want to use Kotlin at any kind of scale you're basically locked to IntelliJ. Baseline Java doesn't carry quite as much baggage.
1
u/Venthe May 12 '26
The utter lack of pattern-matching comparable to Java is also a sore point for me.
I haven't kept up with that particular feature set and I must admit, I'm positively surprised by Java. Neat.
Kotlin is a Jetbrains product (...) if you want to use Kotlin at any kind of scale you're basically locked to IntelliJ
I can't image modern development in the java ecosystem not done in intellij; it's just that powerful :) but you have the point here; for what is worth kotlin is under Apache 2; so if push comes to a shove the language will live on.
2
u/joemwangi May 12 '26
Nullness types are going to be a moot argument once they introduce null-restricted types. This is required for efficient memory layouts of value classes and cache inlining. Both a type system and runtime feature. And they started a prototype experiment. Surprisingly, the design is even better than what C# has. Wonder what will happen to older Kotlin libraries.
4
u/yk313 May 12 '26
Tell me you have no clue about the Java ecosystem without telling me you have no clue about the Java ecosystem.
3
u/joemwangi May 12 '26 edited May 12 '26
Kotlin prioritises syntax over semantics, and that's gonna be a problem in future as java continues to strengthen semantics. Funny enough, same reasons why Vector API is never optimised in Kotlin.
18
u/Worth_Trust_3825 May 11 '26
Virtual threads was just as big. Same with jigsaw. It really shows the monumental work needed to be done this late in the process, but there were more important things to do before this. So it makes sense.
4
u/reflect25 May 11 '26
yay. finally java with the value classes will have an equivalent to the c++/golang structs with the objects being (potentially) allocated on stack rather than always on memory with classes. and be pretty performant. also interesting the immutable/frozen.
i guess mostly this will be used by data structure libraries. https://openjdk.org/jeps/401 (Value Classes and Objects) also looks like this will help the garbage collector.
When an object is flattened or scalarized, it has no independent presence in the heap. This means it has no impact on garbage collection, and its data is always co-located in memory with the referencing object or call stack.
Heap flattening
As an example, the JVM could flatten an array of Integer references so that each array element holds a reference that encodes the underlying integer value directly, rather than pointing to the memory location of some Integer object. Each reference also flags whether the original Integer reference
8
u/Jannik2099 May 12 '26
It's not about the object being on the stack, but about objects being flattened. No more pointer chasing.
1
u/joemwangi May 12 '26
Not really stack allocated. Mainly cpu register allocation which is far optimal.
28
u/Jannik2099 May 11 '26
Very awesome, just 30 years too late :(
10
u/Worth_Trust_3825 May 11 '26
You were never prevented from creating your JNI extension to circumvent this issue.
t. wrote jni to operate on byte buffers in c
1
-4
u/Delta-9- May 12 '26
TIL == in Java is analogous to is in Python, and Java has no analogue to Python's == until this JEP passes.
5
u/woohalladoobop May 12 '26
not true, i believe `Object::equals` is Java's equivalent to Python's `==`.
1
u/Delta-9- May 12 '26
I meant that there is no analogous operator in the syntax.
Object::equalwould be analogous toobject.__eq__, which is called when the==operator is used and conventionally implements a compare-by-value operation; that is,a == bis sugar fora.__eq__(b).This does not appear to be how it works in Java, and I found that surprising. That's all.
-42
u/teerre May 11 '26
I always knew Java was reference based, but this is comical. Ints are boxed by default? What the hell
42
u/davidalayachew May 11 '26
I always knew Java was reference based, but this is comical. Ints are boxed by default? What the hell
No,
intis boxed when you want to use them in generic structures. Which is painful, yes, but this feature is step 1 in changing that.In all other contexts,
intdoesn't get boxed, unless you pass it to a method that was only written to handleIntegerand notint. But that's a choice by the programmer.Once this and a few other JEP's land, then we will be able to use
intfreely in generic constructs, likeList<int>10
u/SpeedDart1 May 11 '26
You’re reminding me of the primitive collection libraries people use in Java land.
IntList, ByteList, etc
5
u/Radi-kale May 12 '26
Don't forget OptionalInt, IntFunction, ToIntFunction, IntUnaryOperator, IntPredicate, IntToDoubleFunction, IntConsumer, IntSupplier, etc. etc.
-18
u/teerre May 11 '26
That's not what the JEP says
The JDK tries to reduce confusion for the immutable classes that model primitive values, such as Integer. In particular, the autoboxing of small int values to Integer uses a cache to avoid creating Integer objects with unique identities. However, this cache, somewhat arbitrarily, does not extend to four-digit int values like 1996: jshell> Integer i = 96, j = 96; i ==> 96 j ==> 96 jshell> i == j $3 ==> true jshell> Integer x = 1996, y = 1996; x ==> 1996 y ==> 1996 jshell> x == y $6 ==> false19
u/davidalayachew May 11 '26
You are attempting to assign an
intto anInteger. That is when boxing occurs.I guess my previous response could have been more clear, but
intboxing only occurs in 2 places in Java atm.
When using an
intfor generics.When trying to use an
intwhen themethodreceiver is expecting anInteger.But again -- it is the programmer's choice to use
Integerinstead ofintin that second case.3
u/hiimbob000 May 12 '26
Integer iandint iare different. Comparing objects by == has always been a footgun-3
9
u/Tarmen May 11 '26 edited May 11 '26
For generic parameters and fields like in
<T> void acceptT(T arg);
Java demands that T is always a pointer. So if you plug a non-pointer type like int in there it must be promoted to the heap-allocated Integer type.
Having a single generic method which always takes a pointer makes things like polymorphic recursion possible. The alternative is to monomorphize dynamically at runtime, e.g. one instantiation per unique call pattern, or to limit programmers to patterns that are easier to statically analyze.
This proposal is sort of tangential to that, and more about giving the jvm more room to optimize. Which sometimes may allow less boxing.
-10
u/teerre May 11 '26
That is terrible but at least understandable. The article mentions that intergers above 1996 do not behave as values even for simple equality. That's a complete breakdown of value semantics and honesty insane
14
u/Dminik May 11 '26
That's not correct. int is the primitive non-boxed, non-referential type. Integer is a boxed type.
Additionally, Integers have an optimization, where a bunch of them are always preinitialized and shared. Many interpreted/scripting languages do this. Some even for primitive values like true/false.
1
May 13 '26
[removed] — view removed comment
1
u/programming-ModTeam May 15 '26
No content written mostly by an LLM. If you don't want to write it, we don't want to read it.
-19
5
u/chucker23n May 12 '26
I’m confused whether this is more like
.NET value types / C# structures; there is no reference/pointer; the memory storage is the value
value objects: can still have a reference, but designed to avoid primitive obsession
I’m guessing the former, but don’t those already exist, e.g.
int? Or is it that those are hardcoded, and this adds writing your own?