What is difference between != and <> usage since both the operators are used for checking inequality in if condition? Is there any performance differences or execution time difference?
They are syntactically equivalent.
However, I'd err towards always using
!= is listed as the inequality operator in the Understanding Expression Operators documentation for Apex.
!= is listed as the Not Equals operator in the SOQL Comparison Operators.
Both are listed in the Formula Operators.
Apex is a Java like language, which in turn is a C-like language. As such I'd follow the Java/C convention of using
!= for not equal to.
Thanks Daniel for explanation. But, my concern is why 2 syntaxes are there since we are accomplishing the same operation with both these operators? Apr 17, 2015 at 9:34
3@PankajGanwani Probably for people who are used to SQL . Apr 17, 2015 at 10:46
I agree with @MrLister and @crop1645. The
<>operator probably came from supporting SQL and Excel like syntax in SOQL and Validaiton rules respectively. It lowers the learning curve for new users by providing a familiar syntax. Apr 18, 2015 at 8:24
It is quite possible that the
<> operator is inherited from Excel's operator language specification as it is commonly seen in Validation Rules (which also support the Excel
Or(..) functions). There was a conscious attempt by SFDC to make Validation Rule formulas (as well as Formula Fields) to mimic Excel to ease user introduction to the SFDC Force.com platform.
As @DanielBallinger states,
<> is not even listed in the Apex doc although it does compile
Same thing different Syntax...
I would also assert that the symbol "!=" more generally represents the idea of inequality (the negation of "equal to"), whereas "<>" implies that the type being compared has a well-ordering.
Can you please elaborate a little bit more on well-ordering term which you have referred in your response? Apr 17, 2015 at 11:33
1A set is well-ordered if any two objects in it can be compared such that one goes before the other, with the only "ties" being objects that are equal. For example, integers (...1 < 2 < 3 = 3 < 4...). Less orderly would be partially-ordered sets (perhaps: head > neck > [left arm | right arm] > [left leg | right leg]...). The least orderly would be unordered sets (maybe the set of all paintings). Typically all data types can be compared for equality, but unless an order comparison operator is defined, they are not well- or partially- ordered. link Apr 18, 2015 at 0:45
Comments are not intended for discussion. Please edit your post to try to make it more clear. And please link to an explicit reference that backs up your claim if you have one. Conjecture is not really what we're after in general on SFSE.– Adrian Larson ♦Nov 29, 2016 at 18:36
<> is not used for comparison in Java. != is.
<> (diamond operator) is used since Java 7 in generic type initialization. For example :
List< string > list = new ArrayList<>;
Apparently both operators doesn't have any performance differences or execution time difference. As they haven't listed it explicitly in documentation.
Source - Salesforce Docs