Equals () and = = are all used for comparison in Java and return a Boolean value, unlike Equals () is a method defined in the object class, = =
is a comparison operator. Here is the source code for equals () in object:
public boolean equals (Object obj) {return (this = = obj); }
We are surprised to find that this method and = = is not the same?? Let's look at the source code for the Equals () method in the String class:
Public boolean equals (Object anobject) { // First, the value of the address is judged, if you want to return directly to true if (This == anobject) { return true; } //if the addresses are not equal, Determine if the object passed in is not an instance of String type if (anobject instanceof string) { //Discovery is an instance of String type, Turn it into a string type (a way to call string after a strong turn) string anotherstring = (String) anObject; //value is a char array inside a string, with each string object corresponding to a char array, to get the length int n = value.length; // Compare the lengths first, and if they are the same, proceed to the next if (n == anotherstring.value.length) { //finds the same length, compares the characters in a char array of string to one by one char v1[] = value; char v2[] = anotherstring.value; int i = 0; while (n-- != 0) { if (V1[i] != v2[i]) return false; i++; } return true; } } return false; }
It is obvious that string overrides the Equals () method so that it has the ability to compare the equality of string content, when equals () is not merely comparing object addresses.
If the address is not the same, he compares the object content to the same, and returns True if the same.
Summarized as follows:
Equals () and = = are used for comparison operations.
Equals () is a method in the object class that is used to compare whether an object is equal and what it means in the object class.
Same as = =, compared to the address of the object, but sometimes we want to compare the object's contents, at this time to override the Equals method, for example, the string class overrides the Equals method by default
Compares the contents of an object.
= = is a comparison operator that, when both sides are basic data types, directly compares the values of two data, and when both sides are objects, the address of the object is compared.
equals and = = in Java