String literal expressions should be on the left side of an equals comparison

17,849

Solution 1

Others have pointed out that the way to avoid this error is to use:

! ("".equals(mapData.get("CON_PTY_PARTY_ID")))

But no one has pointed out why this matters. The reason the literal should be on the left side of the equals comparison is to avoid the possibility of an exception if the string being compared to it is null.

As written in the question, if the value of mapData.get("CON_PTY_PARTY_ID") was null, then the expression would be trying to invoke the equals(..) method of an object that doesn't exist. That would throw an exception. By putting the literal on the left, then even if the value of mapData.get("CON_PTY_PARTY_ID") was null, the method "".equals(...) would be defined and would not throw an exception. It would simply return false.

Solution 2

You shouldn't surround a blank string with quotes. ("").equals(!mapData.get("CON_PTY_PARTY_ID")) should just be ! ("".equals(mapData.get("CON_PTY_PARTY_ID")))

Solution 3

Kevin W.'s answer is correct but has an error:

You shouldn't surround a blank string with quotes.

  ("").equals(mapData.get("CON_PTY_PARTY_ID")) 

should just be

  !("".equals(mapData.get("CON_PTY_PARTY_ID")))

The above is correct.

EDIT:

Source: https://jira.codehaus.org/browse/SONARJAVA-224

Share:
17,849
Wolverine789
Author by

Wolverine789

I am a fresher in a Java Development Project.I want to learn so many things. I want to give 100% contribution to whatever I do....

Updated on June 15, 2022

Comments

  • Wolverine789
    Wolverine789 almost 2 years
    !mapData.get("PARTY_ID").equals("")         // <-- gives SonarQube error
    

    In the above piece of code, I am getting "String literal expressions should be on the left side of an equals comparison" this error in Sonar. So how we can avoid it.

    I tried this:

    ("").equals(!mapData.get("CON_PTY_PARTY_ID"))
    

    But it does not work. Give some advice......

  • krodmannix
    krodmannix almost 10 years
    Yeah, that's not what the OP intended I don't think
  • Wolverine789
    Wolverine789 almost 10 years
    I tried with this one "".equals(!mapData.get("CON_PTY_PARTY_ID")) I am getting this error "The operator ! is undefined for the argument type(s) Object"...
  • Wolverine789
    Wolverine789 almost 10 years
    I tried with this one "".equals(!mapData.get("CON_PTY_PARTY_ID")) I am getting this error "The operator ! is undefined for the argument type(s) Object"...
  • Kevin W.
    Kevin W. almost 10 years
    Use the edited version with !("".equals(mapData.get("CON_PTY_PARTY_ID"))).
  • Wolverine789
    Wolverine789 almost 10 years
    Yes..I tried with this one--> !("".equals(mapData.get("CON_PTY_PARTY_ID"))). ...I am not getting any error...
  • Wolverine789
    Wolverine789 almost 10 years
    But I have a doubt whether !("".equals(mapData.get("CON_PTY_PARTY_ID"))) and ("").equals(mapData.get("CON_PTY_PARTY_ID")) are same or not...I am little bit confused!!!!!!!!!
  • Kevin W.
    Kevin W. almost 10 years
    !("".equals(mapData.get("CON_PTY_PARTY_ID"))) will be equal to !mapData.get("PARTY_ID").equals(""), which is what you originally had. If I were you, I would read into basic Java syntax a little more, as it seems that you're not very familiar with the language.
  • barfuin
    barfuin almost 10 years
    Kevin W.'s answer is correct but has an error - but both of you propose the same solution! Seems to me that Kevin W. was faster ;-)
  • barfuin
    barfuin almost 10 years
    You shouldn't surround a blank string with quotes. - ... with parentheses, you mean. Quotes are ok.
  • krodmannix
    krodmannix almost 10 years
    @Thomas his answer was edited after mine was posted.