Dear Student, how to avoid null verifications in Java

Dear Student, how to avoid null verifications in Java

It is good programming practice, avoid null checking of a value, instead of using if (someObject! = Null), you can use Optional, it can be used as follows:

Optional oR = Optional.of (Optional.ofNullable (yourOriginalInstanceToUse) .orElse (YourInstanceToUseIfTheOriginalIsNull));

If yourOriginalInstanceToUse could be null you should use ofNullable, if you are sure the instance is not null you can use Optional.of

Instead of orElse, you can use orElseGet and pass it a function that returns an object of type YourClass. orElseGet, is much more efficient than orElse.