1st one is better (and the only option), because 2nd one will throw NPE
, when your value is actually null
. As simple as that.
Try this out:
String str = null;
str.equals(null); // will throw `NPE`.
So basically, the test which you wanted to perform itself triggers a NullPointerException
in the 2nd case. So, it is no choice.