Not all equals compare the entire object.
#1. Common Object class:
First look at the source code:
public boolean equals(Object obj) {return (this == obj); }
If no, use "=" to compare the object reference, that is, the address. If the two objects have the same parameters but are created using new, the address is different, and false is returned using equals.
#2. String class:
First look at the source code:
public boolean equals(Object anObject) {if (this == anObject) { return true;}if (anObject instanceof String) { String anotherString = (String)anObject; int n = count; if (n == anotherString.count) {char v1[] = value;char v2[] = anotherString.value;int i = offset;int j = anotherString.offset;while (n-- != 0) { if (v1[i++] != v2[j++])return false;}return true; }}return false; }
Therefore, if the content is the same, use new to create two String objects and call equals to return true.
#3. Integer class
First look at the source code:
public boolean equals(Object obj) {if (obj instanceof Integer) { return value == ((Integer)obj).intValue();}return false; }
The same is the comparison content.