If you are using someone else's code then you're really stuck handling for a possible null
. On the other hand, if you have control over the code base then never return a null
object, and make that a rule across your entire application.
This may sound bad at first but I have developed several enterprise-level applications and this is a very effective way to make the code consistent and much more readable.
So, now, this
if (myString != null && !myString.isEmpty()) {
becomes simply
if (!myString.isEmpty()) {
In lue of that option use the new Optional
feature in J8 as it is intended for that purpose.