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.
-
2Possible duplicate of Is "use "abc".equals(myString) instead of myString.equals("abc") to avoid null pointer exception" already problematic in terms of business logic?gnat– gnat2016年12月15日 08:31:15 +00:00Commented Dec 15, 2016 at 8:31
-
2Possible duplicate of When comparing a string variable to a string literal with .equals(), is there a standard practice for the order of items?Bart van Ingen Schenau– Bart van Ingen Schenau2016年12月19日 14:04:30 +00:00Commented Dec 19, 2016 at 14:04
1 Answer 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.