7 ms·
Although you are technically correct (the best kind of correct), it is still a shortcoming of objects (or maybe just their implementation in Java). It is just
by zeptomu 9y ago
Although you are technically correct (the best kind of correct), it is still a shortcoming of objects (or maybe just their implementation in Java).
It is just a bad idea to implement the '==' operator using comparison on addresses instead of on values and I consider languages pretty low-level that do that.
In a sane language == is always a function adhering the attributes of an equivalence relation (reflexivity, symmetry and transivity), where a user can expect an actual value comparison - AFAIK one is advised to overwrite `equals` in e.g. Java, but I do not think this is good design.
//edit:
There is no obvious answer, as one could argue, that languages just use different names for the same concepts ('==' vs 'equals') and consider comparison by value or by reference more fundamental (and choose '==' for that).
- chrisseaton 9y agoWhy is value-equality inherently more sane than identity-equality, in an object-orientated language? Value-equality takes an unbound amount of time to compute, as well.
- zeptomu 9y agoI agree, that's why I revised my answer (see edit). Probably comparison by-value is more useful in higher-level and comparison by reference in lower-level languages.
- deathanatos 9y agoComparison by value tends to be more useful, and more often what a user expects "==" to mean. In some ways, "==" mirrors the mathematical =. (Not exactly, of course, since mathematics is usually making statements of fact: x = 2 means x is equal to 2, not x is assigned the value 2.) I think Python is a good example here, where referential comparison is the `is` operator: `a is b`.
- hyperpape 9y agoWhat's the meaning of new Long(5) == new Long(5);? I know why the implementation lets me ask that question and I know why it's false, but I'd say that there's no reasonable question I'd ever want to ask using that expression.
- chrisseaton 9y agoYou can think up an unreasonable example for anything though. If I have a graph data structure then `node == node` with identity does make sense to me in lots of cases. And what's the value equality of `(new Object()).equals(new Object())` - they have no value.
- hyperpape 9y agoThe point is that the language treats numbers as objects when they aren't. It's not that reference semantics don't have a place, it's just that the place isn't numbers. As for your second question, it looks like the type system has two embarrassing questions: what the heck does "new Object()" mean? Nothing worth saying.
- chrisseaton 9y agoI use `new Object()` in my code and I use it exactly for identity equality! If I want a unique ID for something I create a new object and it's guaranteed it will never be reference equality to any other object anyone else could construct.
- thaumasiotes 9y agoThere is an obvious answer to which of comparison by value vs "comparison by reference" (I would have called this "comparison by identity") is more fundamental. Comparison by reference is more fundamental than comparison by value.
- zeptomu 9y agoYeah, well you know, that's just like your opinion, man.
- thaumasiotes 9y agoOn the off-chance that you're serious, I'll point out that there is no way to compare equal by identity without also comparing equal by value.
- gvx 9y agoExcept when that value is NaN.
- zeptomu 9y agoCan you elaborate on this? Integer a = new Integer(2); Integer b = a; Here a == b and no comparison by value. Integer c = new Integer(2); Then a != c but a.equals(c) (in Java). My point is that I do not agree with that and would like to have a == c, which would be the case if '==' was implemented using comparison by value.
- LgWoodenBadger 9y agoIt's a technical/pedantic point, but a == b is an value-comparison of the identity "value" (in Java it's the memory reference). Or in other words, how do I know two references are equal (identity equal) without comparing the values of the references? Logical equality devolves to the same thing at the end.
- toast0 9y agoWhile '==' meaning compares equal by value would be nice; it makes the == operator quite complex. If it's a general comparison operator, it would need to recursively compare all the members, and avoid looping on circular references, or risk == running forever. Certainly, one can come up with examples of how a different operator == would be useful for certain types of objects (including Integer), but the architects of Java strongly felt that an operator should always behave the same, regardless of the type of its operands, unless the operator is + and the objects are Strings.
- zodiac 9y agoJust to point out, "equal by comparing addresses" is symmetric and transitive as well. (whether you call it reflexive is an uninteresting question, if you think about it)
- hxtk 9y agoI don't think that it was a specific design decision to make '==' be a pointer comparison instead of value comparison. It's simply a consequence of the much more general design decision that operators cannot be overloaded. `a==b` is always an analog for the C code `a==b`, not `* a==*b` and certainly not C++'s `operator==(a, b)`