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

4

u/agentoutlier Feb 26 '26

Agree.

For me ! is a performance optimization that just happens to give you nonnull characteristics similar to choosing int over Integer. I can see the knee jerk reaction to immediately go around and put ! everywhere but similar to how adding a global flag will break existing code using ! everywhere probably will as well (or just not possible) and can add similar confusion to the global flag.

That is it would be nice to have ! fix the nullability problem but trying to kill too many birds with one stone can lead to a mess.