If you don't talk much about it, let's get started. Let's take a look at the Code:
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 ));
Output answer:
According to the printed results, it can be found that when equal is used for comparison, whether it is to use automatic packing for instantiation or to use new for instantiation, the return value is true, but with = is different, the return value for automatic packing to instantiate is true, and the return value is "new ".
The returned result of the instantiation is true false. First, let's take a look at the difference between equals and =, so that we can know the answer.
The equals method is initially defined in the base class object of all classes. The source code is
public boolean equals(Object obj) { return (this == obj); }
We can see that the equals defined here is equivalent to =, but how can it be different from the above?
The reason is that the string class has rewritten equals:
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 we need to pay attention to five points for equals again:
1 Self-inverse: the return value of X and X. Equals (x) must be true.
2 symmetry: For any referenced values X and Y, if and only when Y. Equals (x) returns true, the return value of X. Equals (y) must be true;
3 pass: If X. Equals (y) = true, Y. Equals (z) = true, X. Equals (z) = true
4. Consistency: If the objects involved in the comparison are not changed, the results of the object comparison should not be changed.
5. non-null: the return value of any non-null reference values X and X. Equals (null) must be false.
After rewriting, there is an essential difference with =:
Equal: used to compare whether the content inside two objects is equal. Because all classes inherit from the java. Lang. object class, if this method is not overwritten, call
Is still the method in the object class, while the Equal method in the object returns the = judgment. Therefore, if the method is not overwritten
Any meaning. In Java object-oriented processing, we generally need to rewrite the equals method in the JavaBean. After Hibernate is used, we need to generate the ing file and entity of the database.
Class, which is the best way to rewrite the equals method in the Entity class. During rewriting, we can implement this method according to our own definitions as long as the five principles are followed. For example, for a student class
We define that as long as the student ID is at the same time we think the two objects are equal; at the same time we also need to override the hashcode method http://www.cnblogs.com/shenliang123/archive/2012/04/16/2452206.html
=: Used to determine whether the addresses of the two objects are the same, that is, whether the addresses of the two objects are the same. Compare pointer operations in the true sense.
To be continued