No more nonsense, come to the point, first look at a piece of code:
Copy Code code as follows:
String str1 = new String ("str");
String str2 = new String ("str");
System.out.println ("= = Comparison:" + str1 = = str2);
System.out.println ("Equal comparison:" + str1.equals (str2));
String STR3 = "str1";
String STR4 = "str1";
System.out.println ("= = Comparison:" + STR3 = = STR4);
System.out.println ("Equal comparison:" + str3.equals (STR4));
The answer to the output:
Depending on what is printed, it is found that using equal comparisons, whether using automatic boxing to instantiate or instantiated with new, returns True, but with = = is different, automatically boxing to instantiate the return is true, and using new to
True false of the returned instantiation; first, to understand the difference between equals and = =, then you can know the answer.
The Equals method is initially defined in the base class object of all classes, and the source code is
Copy Code code as follows:
public boolean equals (Object obj) {
return (this = = obj);
}
It can be seen that the equals defined here are equivalent to = =, but how does the above have to be different?
The reason is that the string class overrides equals:
Copy Code code as follows:
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;
}
Here's a new five point for equals:
1 reflexivity: The return value of X,x.equals (x) for any reference value must be true.
2 symmetry: For any reference value X,y, the return value of X.equals (Y) must be true if and only if the Y.equals (x) return value is true;
3 transitivity: If X.equals (y) =true, y.equals (z) =true, then X.equals (z) =true
4 Consistency: If there is no change in the object being compared, there should be no change in the outcome of the object comparison
5 Non-nullability: The return value of any Non-null reference value x,x.equals (NULL) must be false
After rewriting, there is an essential difference with = =:
Equal: is used to compare the contents of two objects within the equality, because all classes are inherited from the Java.lang.Object class, so if the method is not overridden, call the
is still the method in the object class, and the equal method in object returns the judgment of = =, so if the method is not overridden, then calling the method is not
of any significance. In Java object-oriented processing we generally have to choose to rewrite the Equals method in the JavaBean, after using hibernate, we will generate the database mapping file and the entity
Class, which is the best way to rewrite the Equals method in the entity class, we can implement the method according to our own definition as long as we follow the Five principles, for example, for a student class
We define the equivalence of these two objects as long as we are studying the number, and we also rewrite the Hashcode method http://www.jb51.net/article/42408.htm
= = is used to determine whether the address of two objects is the same, that is, whether it refers to the same object. Comparison is the real sense of the pointer operation.