Read Catalogue
- Equals ()
- The Equals () method and the "= =" operator Compare
Return to the top of equals ()
There is this equals () method in the Superclass object, which is used primarily to compare the equality of two objects. The source code of the method is as follows:
public boolean equals (Object obj) { return (this = = obj); }
We know that all objects have identities (memory addresses) and states (data), while "= =" compares the memory addresses of two objects, so the Equals () method using object is to compare the memory addresses of two objects for equality, even if Object1.equals (object2 True indicates that EQUALS1 and EQUALS2 actually refer to the same object. Although sometimes the Equals () method of object satisfies some of our basic requirements, we have to be clear that most of the time we are comparing two objects, this time the Equals () method of object is not allowed, in fact the JDK, String, Package classes such as Math override the Equals () method. The following is the Equals () method of 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; }
For this snippet: if (v1[i++]! = v2[j++]) return false; we can see very clearly that the Equals () method of the string is a content comparison, not a reference comparison. As for the other packaging classes are similar.
Go back to the top Equals () method and the "= =" operator comparison
First of all, generally speaking, "The Equals () method in Java and the" = = "operator" are all compared addresses , so why do we always confuse the use of the situation is always wrong, because the "override Equals () method" and some " The existence of a special case ".
There are two usage notes:
( 1 for string variables, comparing strings by using the "= =" and "Equals ()" methods differs in comparison methods.
"= =" compares the value of two variables themselves, that is, the first address of two objects in memory.
Equals () compares what is contained in the string.
Like what:
String S1,S2,S3 = "abc", S4 = "abc";
S1 = new String ("abc");
S2 = new String ("abc");
So:
S1==S2 is false//Two variable memory address is not the same, that is, they point to the object is not the same, it is not equal.
S1.equals (S2) is true//Two variables that contain the contents of ABC, so it is equal.
Note ( 1):
if: stringbuffer s1 = new StringBuffer ("a");
StringBuffer s2 = new StringBuffer ("a");
Result: s1.equals (S2)//Is false
Explanation: The Equals method isnot redefined in the StringBuffer class, so this method comes from the object class, and the equals method in the object class is used to compare "address ", so it equals false.
Note ( 2):
For S3 and S4, it is a bit different to note that because S3 and S4 are variables generated by two string constants, where the memory address is equal, s3==s4 is true (even if there is no s3= S4 such an assignment statement)
( 2 for non-string variables, the"= =" and "equals" methods have the same effect to compare their object at the first address of the heap memory, which is used to compare whether two reference variables point to the same object.
Like what:
Class A
{
A obj1 = new A ();
A obj2 = new A ();
}
Then:Obj1==obj2 is false
Obj1.equals (OBJ2) is false
But add such a sentence:obj1=obj2;
So Obj1==obj2 is true
Obj1.equals (OBJ2) is true
In summary: Theequals method is the comparison for a string, and for a non-string comparison, the object to which it points is the same.
The = = comparison is also the same as the object that the object is pointing to is the first address in memory.
The Equals method is redefined in the string class , and the value is compared instead of the address. So it is true.
Attention:
- If it is the basic type comparison, then can only use = = To compare, cannot use equals
public class Testequals {public static void main (string[] args) {int a = 3;int b = 4;int c = 3; System.out.println (A = = B);//The result is falseSystem.out.println (a = = c);//The result is trueSystem.out.println (A.equals (c));//Error, Compilation cannot be passed, Equals method//cannot use comparison with base type}}
- For a basic type of wrapper type, such as a Boolean,Character,Byte,Shot,Integer,Long,Float, double, andother reference variables, = = is the comparison address, and equals is the comparison content.
public class Testequals {public static void main (string[] args) {integer n1 = new Integer (+); integer n2 = new Integer (30 ); integer n3 = new Integer (31); SYSTEM.OUT.PRINTLN (n1 = = N2);//The result is false two different Integer object, so its address is different, System.out.println (n1 = = n3);//Then either the new Integer (30) or the new Integer (31) results are shown falseSystem.out.println (N1.equals (n2));//result is true according to the instructions in the JDK document, the contents of N1 and the objects pointed to by N2 are equal, all 30, The result of equals comparison is TrueSystem.out.println (N1.equals (n3));//result is false because the object content is different, one is 301 is 31}}
This is an example of an integer, if it is other such as Double,Character,float and so on.
From:http://www.cnblogs.com/qian123/p/5704007.html
Java improved--equals () method and "= =" operator