the "= =" number, which compares the address value of an object in memory,
such as 2 string objects
string S1 = new String ("str");
String s2 = new String ("str");
If you compare it with the = = number, it will return false, because two objects are created and they are in a different location in the in-memory address.
The case of equals is more complex, and it is a method in the Java.lang.Object class. Because all classes in Java inherit from object by default, all classes have this method.
This is written in the object source code.
public boolean equals (Object obj) {
return (this = = obj);
}
He also uses the = = number to compare memory addresses. But this method is overridden in many Java classes, such as String.
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;
}
In string, if the = = number is not equal, a comparison of the values is performed.
So the exact function of the Equals method depends on how the current class is implemented to override the method in the parent class. If the method is not overridden, then he and the = = number are equivalent.
whether the two variables represented by the equals operation are references to the same object, that is, whether the contents of the heap are the same.
The difference between equals and "= =" in Java