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

1

u/koflerdavid Feb 27 '26

The issue about making it per module or per project is that it would be quite hard to see what is valid for the current file since you would have to look into package-info.java, module-info.java, and then additionally into the project configuration.

Haskell works like this, but that language is supposed to be a testbed for research, which means there are a lot of whacky language extensions. But anybody using Haskell in production tamps down hard on that diversity, and since Java is intended for production use first and foremost, a similar policy applies.