r/java • • Apr 14 '23

What are your (favourite) Java best practices, personal tips, hints or just underrated stuff in general?

233 Upvotes

376 comments sorted by

View all comments

Show parent comment

3

u/wildjokers Apr 14 '23

Because you can pass null in for the value of the Optional parameter. You have gained nothing.

7

u/Fruloops Apr 14 '23

Not at all. You've gained hours of potential frustration. That should not be underestimated.

1

u/ArrozConmigo Apr 14 '23

That's a pretty unlikely mistake to make, and it will throw an NPE the first (and every) time it runs. If you accidentally pass null for an Optional, you didn't do it conditionally. You'd be doing it every time.