0

why is "".equals(someString); safer than someString.equals("");?

I understand that for example if someString is initialized to null then the second gives a null pointer exception while the first will run. Is this the only advantage?

blalasaadri
  • 5,990
  • 5
  • 38
  • 58
AmericanSuave
  • 363
  • 3
  • 4
  • 14
  • 2
    I think so... preventing Null Pointer Exceptions. – gtgaxiola Jan 07 '15 at 18:13
  • 1
    That is the only advantage I'm aware of. It should also be noted however that this can make your code a little less stable because a value of null for a String can indicate a deeper problem in some situations. The "safer" approach outlined above will not give you any indication when this problem exists. – M7Jacks Jan 07 '15 at 18:41

0 Answers0