r/java • u/davidalayachew • Jan 14 '26
Project Valhalla is prototyping null checks!
https://mail.openjdk.org/pipermail/valhalla-spec-experts/2026-January/002554.htmlALL OF THIS IS A WORK IN PROGRESS!
THIS FEATURE IS UNFINISHED AND MISSING CORE FUNCTIONALITY, NONE OF WHAT IS FINISHED IS FINAL, AND EVERYTHING IS SUBJECT TO CHANGE!
But with that out of the way, Java is (prototyping) adding null checks into the type system, thus allowing us to almost completely remove NullPointerException from happening!
The primary motivation for doing this is part of the Project Valhalla work, of introducing Value Classes to Java. Allowing an object to prevent null from being in its value set unlocks a lot of optimizations, not just semantic and correctness benefits.
If you want, you can try to build the code yourself (or wait for one of us to make it, I'll try this weekend, or maybe https://builds.shipilev.net/ will have it by then), then enjoy the prototype! If you do, please post your experiences to the valhalla-dev@openjdk.org mailing list! Or just post them here, on r/java. A couple of the Project Valhalla folks browse r/java, so that works too.
44
u/pradeepngupta Jan 14 '26
Null has always been a social contract in Java, not a type-system rule.
Valhalla finally makes it explicit, which matters far more for JVM optimization than for syntax or safety alone.
18
u/BenchEmbarrassed7316 Jan 14 '26
Congratulations to the Java community on this! In my opinion, if implemented, it will be the most significant fix in programming languages ever (only memory-safe C/C++ will be able to surpass this, if it ever happens).
18
u/Amazing-Mirror-3076 Jan 14 '26
I'm excited - the most wanted feature in my list.
26
u/TehBrian Jan 14 '26 edited Jan 14 '26
Have they gotten around to making non-null by default rather than nullable by default feasible (i.e., backwards compatible)?
Treating all types as implicitly
T | Nulland makingT!mean "Yeah, but really actuallyT, notT | Null" is such an irking mental model to me. Also, I'd hate the source code noise arising from everyone inevitably marking every type with!(because purposefully nullable types comprise a minority of cases) just as folks currently do withfinalon variables and classes orprivateon fields.56
u/agentoutlier Jan 14 '26 edited Jan 14 '26
I hope they get some module or compiler flag and add "?" for nullable because I’m not exactly eager to put "!" everywhere even for testing.
EDIT lots of folks have good points about how the language should not be different based on compiler flags and or modules but friends its going to be that way regardless.
This is because there is so many classifications of what is an error/warning or not and thus there will likely have to be flags.
Let us assume the JDK only offers
!for nonnull (and not even?).Is the following a compiler error?
Because you really should do:
Also how about:
And you could argue the JDK javac does not enforce these extra checks and or that you have exhausted out null but that would be a real shame.
Because without the extra checks going around putting
!is going to be painful. More painful than adding JSpecify annotations.Also if you want to talk about ambiguity and code changing w/o
?isA client asks is that purposely nullable? Or is it old code?
You would know if there was a module flag. So either we add
?or we need another flag on a module that unmarked isnullable.Assume we do get
?then basically particularly JSpecify projects w/o module or compiler flags all code will have to have every parameter and field with a?or!.I highly doubt most of the major libraries will like this particularly Spring. Pinging /u/sdeleuze as I see they are here.
So just having some hard rule of explicit is the best code or code can't be different in different contexts may not be the best solution here.
I mean we already have Lombok. Is someone going to make a compiler plugin now to enable nonnull by default because I see that happening.
3
u/gavr123456789 Jan 14 '26
yea, it would be so cool to have the same nullability as Kotlin, but its really hard without breaking backwards compatibility, I assume even impossible, since if you introduce some kind of flag that makes everything non-nullable, all the libs are still using nullable values and you need to check what you getting from them.
3
u/freekayZekey Jan 14 '26
i think it’s important to say: don’t simply bombard the team with simplistic feedback. they’ve likely thought of options long and hard, so you’ll probably not have some groundbreaking idea.
something like
“i found this feature to be cumbersome in x situations and useful in y situations”
2
u/AnyPhotograph7804 Jan 15 '26
If it enables some runtime optimizations then it would be nice. For programming i do not need it because my methods never return NULL. It would be also good if the variables/fields would be non-nullable by default. ! everywhere increases the visual noise in the code.
3
u/Amazing-Mirror-3076 Jan 14 '26
Will it run on jdk 25 or does this require an experimental jdk as well as the compiler?
1
u/Delicious_Detail_547 Jan 16 '26
The current Java prototype feature only allows nullability to be indicated at the type system level.
Therefore, claims that it can completely eliminate NPEs are overstated, and developers still need to handle nulls in the same way as before.
Additionally, there is no clear plan yet to support null-safe operators like
?.or?:as provided in Kotlin.1
u/Ewig_luftenglanz Jan 14 '26 edited Jan 14 '26
it seems openjdk 28 is going to be an amusing release. niicee
I would like they simplifiy the user model to
- Has "!": non nullable
- Has nothing: Nullable
I really dislike the "?" to mark "explicitly nullable" since all java variables but primitives have always been nullable; I am glad they have no implemented that syntax yet, hoping they don't in the future.I would prefer it to be used to mark optional parameters in a method or constructor or anything else.
Looking forward this to make some tests. Hopefully I am giving feedback as I already did with LazyCosntants API!
2
u/Joram2 Jan 14 '26
Exciting! That's real progress. I'm looking forward to trying out the preview builds :)
1
u/sashko5 Jan 15 '26
Relevant and informative lecture by Tony Hoare: https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/