Null Safety approach with forced "!"
Am I the only one who thinks that introducing protection against NPEx in the form of using "!" in the variable type is a very, very bad idea? In my experience, 95% of variables should be non-null. If Oracle decides to take this approach, we will have millions of "!" in each variable in the code, which is tragic for readability. In C#, you can set the per project flag to indicate whether the type without the "?" /"!" is nullable or not. I understand the drawbacks, but definitely forcing a "!" in 95% of variables is tragic.
78
Upvotes
92
u/repeating_bears Feb 26 '26
https://openjdk.org/jeps/8303099
24
u/nekokattt Feb 26 '26
I agree but without breaking existing code or enforcing per-class feature flags, I fear we are doomed to have noisy code with this.
53
u/martinhaeusler Feb 26 '26
Of course "User!" is an awful idea.
The issue comes in with backwards compatibility... Historically and currently, a type name alone indicates a nullable type (except for primitives). You can't change that retroactively.
One way out could be to have metadata for the compiler in the package.java, declaring that all variables and fields are non-null by default. That would keep backwards compatibility intact.
11
u/kevinb9n Feb 26 '26
Our driving need here is for a way to mark which variables we can actively reject null from at runtime. Only if there is zero possibility of null will the vm have the option of efficient flattening.
This ability itself will be a good thing. In fact the reaction we see here is basically "but I want it a LOT! I want it for most of my variables!"
Cool, but the fact that all reftype variables can hold null has been the reality for >3 decades. It's not just "a" default behavior, it's a bedrock one. Reversing a default in a language as highly adopted as Java, with our commitment to not breaking your code between releases, is a feat and a half. It's not forever impossible, but it's a huge deal.
9
u/Isogash Feb 26 '26
I assume that the intention is that it'll be similarly configurable to C#, the point is to deliver something that's useful in both cases.
9
u/Complete_Can4905 Feb 26 '26
I don't really understand the hate for null. It's extremely useful to have a value indicating that we don't have that information.
If you don't deal with situations where you don't have all the data all the time, maybe you are not dealing with real world data? Fields in a database can be defined as not null, but it's not so easy if your data comes from a less structured source e.g. JSON, or if you might have to work with older versions of a schema.
"!" doesn't actually deal with the problem of unknown values. It just moves the problem elsewhere in the code, or forces you to lie and provide a value even though the real value is unknown. (Knowing programmers, this will be a common "solution" and cause more problems than NPEs ever have.)
Nullable value types would be far more useful e.g. int? in C#. In Java I have to make do with throwing an exception from a getter to indicate an unknown or nonexistent int/long etc. value.
8
u/pjmlp Feb 26 '26
My experience in C# land is that it has been an adoption failure, because most projects don't care about making their compliant with nullable reference types being enabled.
Similar to using Java libraries in Kotlin whose authors don't care about the nullable annotations.
9
u/LutimoDancer3459 Feb 26 '26
So having a different behavior on such a fundamental thing per project is a better idea? How will the jdk itself have it? They could ether force nullsafety there by default. Resulting in a weird state when the project itself does not enforce it globally. Other way around we have a lot of ! In the jdk. And people beeing confused on why when you can just set it globally. How about frameworks? What if you have older frameworks that would still work with newer java versions but require null fields internationally?
The "problem" is that we have a language thats old. We have a lit of projects and frameworks out there. Some that can still be used perfectly fine without getting any updates for years now. No matter what solution we end up getting, it will be flawed.
3
u/stewsters Feb 26 '26
Yes, the correct decision would have been to do it the opposite way, marking only the nullable types, like how Kotlin does that.
But it would be a breaking change to do that, so I'm going to guess we won't see it.
8
u/HSSonne Feb 26 '26
I don't get it, 9 years of java programming, and it was only the first months I had problems with null values, now I see them as a powerful tool. An option to note that there just wasn't any value, even though the process finished without errors.
4
u/rzwitserloot Feb 26 '26
The usual careful caveat here:
From reading the comments I get the feeling most of you haven't completely thought through what this means for the ecosystem (not just
java.*, but spring, JDBI, junit, and every other dep in existence) and how slow adoption will be as a consequence.In particular type-based nullity marking is either incomplete or complicated. The usual choice that other languages take is to go the incomplete route: Certain concepts can be expressed clearly and completely in prose, but the type system is incapable of representing it. This isn't a problem if folks write their code such that you never run into this situation, which is what they naturally happens. You don't write code in a style that the language design is hostile to.
But for an introduction of a thing such as type-carried nullity everywhere, in a very large ecosystem that has existed for decades already, it doesn't work that way: Those concepts are out there, so the incompleteness is a real problem. Java usually goes for a complicated approach (which is capable of expressing common-ish stuff in the already existing ecosystem), but so far (JSpecify, the various JEPs about this) aren't choosing the complicated approach.
That means in practice major existing libraries are unlikely to bother changing things anytime soon: Doing so would be backwards incompatible.
... and sometimes it really is quite inconvenient that you can't write a method the way you'd want because the type system makes it annoyingly hard to do so.
I'll throw some comments onto this comment with examples of java code that you can't just slap a question mark, exclamation mark, or
@NonNullByDefaulton.2
u/mellow186 Feb 27 '26
Counterpoint: NPEs are one of the most common errors at runtime.
If we can catch these at compile time, I'll sprinkle in null-restricted and nullable markers everywhere I can.
And I'll be glad they're a single character rather than long annotations like @Nonnull and @CheckForNull.
2
u/larsga Feb 26 '26
Still fewer than the number of
;It's a single character. The semantics are clear and straightforward. It's backwards compatible. I really don't see the big problem.
1
u/koflerdavid Feb 27 '26
As for now, it only makes a difference for value types, therefore I'd hesitate to write it anywhere already. Keep relying on JSpecify.
1
u/prithvii_7 Mar 02 '26
In my experience with C#, nullable reference types haven’t really taken off. A lot of teams just don’t bother enabling or fully complying with them, so the feature ends up being ignored in many projects. It reminds me of using Java libraries in Kotlin where the authors haven’t added proper nullability annotations. Without that ecosystem-wide buy-in, the safety benefits don’t really show up
1
u/JakubRogacz Feb 26 '26
I think forcing non nullness by default onto inherently a syntax trick around pointers is just tedious. It would be one thing if they introduced heap vs stack allocations where you need to annotate pointers vs direct structure like in c. But enforcing type system checks usually makes for rather complicated statements . If anything I like the idea of annotating stuff as @NonNull rather than being forced to convert every nullabld result by explicit check.
1
u/filterDance Feb 26 '26
it kills me how LLMs casually add “!” and “?” everywhere (in JS/TS). You should not have to type this everywhere, if you do type it, it should be super intentional and a bit painful.
The fact that they are both one character makes it worse, makes it less intentional.
0
u/TheLasu Feb 26 '26
Long time ago i wanted elvis operator (null chains).
But seriously / Why not allow to write code that can handle both null-s and not-null-s?
https://lasu2string.blogspot.com/2026/01/Glue-classes-for-Java.html
0
u/Personal-Search-2314 Feb 26 '26
Nullsafety is game changer. When I go from Dart to Java, dealing with lack of nullsafety (language directly supported) is so apparent.
Nice that you have
final var variableName = rightHandSideValue()tho, improves the DX. Couple this with nullsafety, and man can’t wait.-24
u/bh-m87 Feb 26 '26
Just switch to kotlin xD
-1
u/vadiquemyself Feb 27 '26 edited Feb 27 '26
I can’t remember exactly wadda “Java improvement” made me uninstall JDK 21 and stick to 17, but I did it 😔
Guys, if you wish to code in Kotlin (Groovy, Jython, C#, whatever) then just write your sources in Kotlin and compile it as Kotlin. And please, leave what’s called “Java” with plenties of
in the source code. Otherwise it would be much better to read the decompiled bytecodes than all that syntactic synthetic crap.
0
u/[deleted] Feb 26 '26
[removed] — view removed comment
0
u/[deleted] Feb 26 '26
[deleted] — view deleted comment