Earlier quoted context omitted.
Can 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.
> 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
> 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.