-2

I have a question about the object X.equals(Y).

I use Sonar and it says that I have to move the "" string literal on the left side of this string comparison: !date.equals("").

So I did that: !("").equals(date) but I don't really know if it is right or not.

Tulains Córdova
  • 39,201
  • 12
  • 97
  • 154
Fosfor
  • 19
  • 1
  • 2
    Possible duplicate of [Is "use "abc".equals(myString) instead of myString.equals("abc") to avoid null pointer exception" already problematic in terms of business logic?](http://softwareengineering.stackexchange.com/questions/313673/is-use-abc-equalsmystring-instead-of-mystring-equalsabc-to-avoid-null-p) – gnat Dec 15 '16 at 08:31
  • 2
    Possible duplicate of [When comparing a string variable to a string literal with .equals(), is there a standard practice for the order of items?](http://softwareengineering.stackexchange.com/questions/147650/when-comparing-a-string-variable-to-a-string-literal-with-equals-is-there-a) – Bart van Ingen Schenau Dec 19 '16 at 14:04

1 Answers1

1

!"".equals(date) and !("").equals(date) and !(("")).equals(date) and !(((""))).equals(date) and !("".equals(date)) all return the same thing.

So you correctly switched the string literal with the variable, you just added some not needed parentheses.

Darsstar
  • 266
  • 2
  • 6