8

Is there any difference between using <> and != for checking if two native objects are not equal to each other in Apex? i.e. String, Date etc.

I have only ever used != which has never failed me before.

1
  • 2
    Keep using it, comes down to preference more than anything else. You will find it hard to nail down Salesforce documentation using or recommending <>. Daniel left a good answer with hyperlinks here. – TSmith May 7 at 13:33
10

There is no practical difference, just different symbols for the same operation.

Do note that <> is not documented as an expression operator in Apex.

While you can use either, I'd prefer != as it's documented and follows the principle of least surprise. Whatever you end up using, just be sure to us it consistently (don't switch between the two).

3
  • 4
    Consistency is king. – Adrian Larson May 7 at 13:46
  • 1
    At some point we were creating some Development Best Practices (both for devs and delacartives). There was huge fight over <> and != because most of the declarative devs used <> for some reason (I think that initially trilaheads were teaching to use <> with formulas etc. In the end we agreed to use always != as all of the devs said that <> is abomination. And because consistency is a king... :) – user1974566 May 7 at 15:30
  • 1
    Worth noting that <> is also not documented as an operator in SOQL. – Adrian Larson May 7 at 18:55
2

<> is/was historically used in both Microsoft Excel formulas and various SQL languages. To offer developers and administrators alike a familiar experience, Apex, SOQL, SOSL, and formulas allowed the use of either <> or !=. While apparently (at least partially) undocumented now, the <> operator has been supported since the very first version of their respective technologies (formulas, SOQL/API, and Apex).

While you can use either, it is idiomatic to use != when writing new code, new formulas, and new API integrations. Having gone undocumented, it is likely that salesforce.com would like to eventually remove support for this operator in favor of the more standard != operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.