In Java, the == operator was used to ensure security with all combinations of operands that would be compiled, and provided that none of the operands were float or double , it would implement an equivalence relation. With the advent of autoboxing, this is no longer true, because unfortunately, legal comparisons between primitives and their wrapped equivalents are unsafe and extend the == operator in a way that is incompatible with equivalence relations defined by individual types.
While the equivalence relation that Java will check when both operands are reference types (for example, reference equivalence) was not always one of those interested in programmers, the == behavior corresponded to reference types. If the variables x, y and z are any combination of reference types, and the expressions x == y, y == z and x == z are compiled, then in each case when two are true, the third will also be.
In C #, the token == used to represent two different operators: an overloaded equality test and a default equality test. If one of the operands in == defines an overload that is applicable to a specific combination of the provided operands, the token will be interpreted as an overloaded equality operator. Otherwise, if the operands are compatible reference types, this will be interpreted as the standard test operator for setting equality. Since the same token is used for both operators, and they have different semantics, it cannot generally implement a reliable equivalence relation among reference types if none of the considered types overload it to represent anything other than reference equality .
Given the definitions of String s1=123.ToString(); String s2=123.ToString(); Object o = s1; String s1=123.ToString(); String s2=123.ToString(); Object o = s1; comparing s1==s2 will return True (using the overload (string,string) == , which sees that they contain the same characters), and o==s1 will return true (using the default referential equivalence operator to see that they are repeatedly the same object), but o==s2 will return false (using the default referential equivalence operator to see that they are different objects).
If both operands before == are of type Object , it will behave as an equality control test; however, if they belong to other types, then the operator can sometimes behave as a control test of equality, and sometimes as something else.
supercat Apr 16 '14 at 17:47 2014-04-16 17:47
source share