4 ms·
Yeah, well you know, that's just like your opinion, man.
by zeptomu 9y ago
Yeah, 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.
- thaumasiotes 9y ago> Here a == b and no comparison by value. Um, nonsense? class Intbox { public static void main(String[] args) { Integer a = new Integer(2); Integer b = a; System.out.println(a == b); System.out.println(a.equals(b)); } } $ java Intbox true true
- grzm 9y agoI believe what your parent is emphasizing the difference between comparing a == c and a == b. Both are identity comparisons as opposed to value comparisons. > cat intbox.java class Intbox { public static void main(String[] args) { Integer a = new Integer(2); Integer b = a; Integer c = new Integer(2); System.out.printf("a == b => %b\n", a == b); System.out.printf("a.equals(b) => %b\n", a.equals(b)); System.out.printf("a == c => %b\n", a == c); System.out.printf("a.equals(c) => %b\n", a.equals(c)); } } > javac intbox.java > java Intbox a == b => true a.equals(b) => true a == c => false a.equals(c) => true Edit to add: Your point regarding that "there is no way to compare equal by identity without also comparing equal by value" is true but doesn't really say much. (I'm not sure if it isn't a tautology?) There are cases where one might want to know whether two objects are the same object, or whether they represent the same value. Which you want depends on context. That's separate from the decision Java made that == was for identity comparison. Some people disagree with that decision.
- thaumasiotes 9y agoThe point is that equality of identity is objectively more fundamental than equality by any other metric. You can easily observe this by noting that any two objects which are equal in identity are necessarily equal in all other possible ways. There is no argument to be made that "comparison by value is just as fundamental a concept as comparison by identity", or that this is just a matter of opinion. The fact that it is possible to compare equal by value without comparing equal by identity does not contradict that it is impossible to compare equal by identity without also comparing equal by value, however you define "equal by value". > Your point regarding that "there is no way to compare equal by identity without also comparing equal by value" is true but doesn't really say much. (I'm not sure if it isn't a tautology?) It's not a tautology; it is one of three requirements defining an equivalence relation. (Equivalence relations are by definition reflexive, which is to say x is equivalent to x for all x.)
- Sean1708 9y agoI don't know Java so I may have missed something, but isn't this[0] a counter-example? [0]: https://ideone.com/w8NhmR https://ideone.com/w8NhmR
- thaumasiotes 9y agoNo, that is not a counterexample, because while that code defines a method named "equals", the method so defined is not an equivalence relation.