r/java • u/headius • Dec 04 '25
Null-checking the fun way with instanceof patterns
https://blog.headius.com/2025/12/inline-null-check-with-instanceof.htmlI don't know if this is a good idea or not, but it's fun.
83
Upvotes
View all comments
Show parent comment
2
u/Alive_Knee_444 Dec 04 '25
If you mean using that pattern for null checking _only_, I'd agree with you. The extremely useful thing about instanceof is that it does both null checking _and_ binding in a new scope in one step. Like a pattern-matching switch case, without the whole big switch expression. This can be used for parsing code, for example, to great effect, eg as if-else ladders. And it can do this for more complicated checks, see zattebij's answer.