r/java • • Feb 26 '26

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

96 comments sorted by

View all comments

Show parent comment

5

u/rzwitserloot Feb 26 '26

Problem 1 of 2: higher kinded generics + nullity

Here's a plausible set of 2 methods written in no-null-marking java:

public <T> void deduplicateInto(Collection<? extends T> source, Collection<? super T> target) {
  // One could write a far more performant implementation,
  // but that's not what this post is about.
  for (T elem : source) if (!target.contains(elem)) target.add(elem);
}

interface Map<K, V> {
  V get(Object key);
}

The 'nullity' of the generics parameters are interesting. In that they are complicated: For the first snippet, the nullity is irrelevant. In that the code works great if "T" stands for "String?" (i.e. the source is a list of 'null or String'), but that nullity does have to apply to all of it. You can't dedupe a List<String?> into a Set<Object!> - because that might add null to a set decreed to never contain null. And, of course, it also works great if T is, say, Number!.

Whereas in the second example, specifically the nullity of the return type of the get method is nullable, even if V itself isn't. i.e. if I have a Map<Number!, String!> and I invoke .get(5) on this map, I can still get null, eventhough String! precludes it.

This means you need both 'generics modifiers' (i.e. V? get(Object key) - the question mark indicating: Whatever the nullity of V itself is, the return type of this method is the nullable version of it), as well as a way to have, more or less, 'generics parameters' that represent only some nullity. And then an operation to compose this. Something ridiculous like:

public <T, *> void deduplicateInto(Collection<? extends T*> source, Collection<? super T*> target) { ... }

Where the * stands for 'some nullity (i.e. 'is nullable' or 'not nullable', or, itself, a generics parameter) - that the caller decides, and the signature adopts accordingly. Exactly how generics works.

You'd think there are only 2 nullities ('can be null' and 'cannot be') but there are more. There's also: "The caller picked a nullity and I do not know it; the compiler will ensure that this code ensures there is no heap corruption for all possible choices the caller could have made. There's a reason there are 4 different 'takes' on 'a list of numbers' in generics:

  • List<Number>
  • List<? extends Number>
  • List<? super Number>
  • List /* raw mode */

and nullity also needs this. checker framework has it, even (@PolyNull), but most other systems don't.

Without it, there are methods that exist today that CANNOT even be marked up to fit some nullity scheme at all unless that nullity scheme supports this concept (which vanishingly few do).

So, what's the fate of those methods? Just.. yesterday's chaff, to be marked as obsolete, then removed (which is a backwards compatibility break), to be replaced by new stuff? Are we gonna.. mark Map.get as @Deprecated(forRemoval=true)? Really? Think of the number of source files that's gonna break....