== 、equals 、hashcode

 == 
Comparison basic data type value; 
. Comparison of reference type is an address value.

 the equals (Object O)
. 1) can not compare the basic data types, data type is not base class type; 
. 2) when the comparison reference type A (the method inherited from Object, the object is to compare address value) is equivalent to the "==" ; 
method Object class, so that, in each class in a java, this method will have, a java class because each is directly or indirectly subclass Object class, this method will be inherited.

2) b. If they have been written class overrides the equals method, then the user-defined installation to compare whether two objects are equal, if not rewritten equal method, it will call the parent class (Object) the equals method comparison, is a comparison of address values.

Note: Some implementation class (JDK), the rewrite equals method, this time comparing the content (java.lang.String) 
in a custom class, if the comparison object, they can override the equals method defined comparison rules.

Note: equals (Object o) method can only be an object to call, and then also to pass a parameter object.

So the following wording is wrong: 
int = A. 1; 
a.equals (. 1); 
as basic data types are not considered an object, the method can not be called.

1) If the basic data types then compare with == 
2) if it is a reference type, we want to compare their own way, it is necessary to rewrite the equals method in this class, 
if not rewritten, and it equals == Compare the effect is the same, all the address comparison reference value. 
3) If it is a string, then directly equals on it, because the String class which has overridden the equals method 
when comparing the contents of the string, but the value of the address is not referenced.


--------------------- 
Original: https: //blog.csdn.net/qq_42857603/article/details/81606707 

equals

Object,

  public boolean equals(Object obj) {
        return (this == obj);
    }

Obviously two objects is address value comparison (i.e., the comparison reference is the same). But we know, String, Math, Integer, Double, etc. These wrapper classes in the use of equals () method, has been covering the equals () method object class.

String in

 1  /**
 2      * Compares this string to the specified object.  The result is {@code
 3      * true} if and only if the argument is not {@code null} and is a {@code
 4      * String} object that represents the same sequence of characters as this
 5      * object.
 6      *
 7      * @param  anObject
 8      *         The object to compare this {@code String} against
 9      *
10      * @return  {@code true} if the given object represents a {@code String}
11      *          equivalent to this string, {@code false} otherwise
12      *
13      * @see  #compareTo(String)
14      * @see  #equalsIgnoreCase(String)
15      */
16     public boolean equals(Object anObject) {
17         if (this == anObject) {
18             return true;
19         }
20         if (anObject instanceof String) {
21             String anotherString = (String)anObject;
22             int n = value.length;
23             if (n == anotherString.value.length) {
24                 char v1[] = value;
25                 char v2[] = anotherString.value;
26                 int i = 0;
27                 while (n-- != 0) {
28                     if (v1[i] != v2[i])
29                         return false;
30                     i++;
31                 }
32                 return true;
33             }
34         }
35         return false;
36     }

Obviously, this is more content, but is no longer comparing addresses. And so on Math, Integer, Double, etc. These classes are rewriting the equals () method, which is carried out relatively content. Of course, the basic type of a value comparison.

hashcode

object in

   public native int hashCode();

Description is a native method, its implementation is relevant according to the local machine. Of course, we can override the hashcode () method to write their own classes, such as String, Integer, Double, etc. These classes are covered hashcode () method. Such as those defined in the String class hashcode () method is as follows:

 

/**
     * Returns a hash code for this string. The hash code for a
     * {@code String} object is computed as
     * <blockquote><pre>
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     * </pre></blockquote>
     * using {@code int} arithmetic, where {@code s[i]} is the
     * <i>i</i>th character of the string, {@code n} is the length of
     * the string, and {@code ^} indicates exponentiation.
     * (The hash value of the empty string is zero.)
     *
     * @return  a hash code value for this object.
     */
    public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }

 

 

The default is the same as equals and == determine references are equivalent, the general need to achieve real equality has to be overwritten later.

If you think that two objects are equal, then make the best hashCode value of these two objects is equal, because in addition to the object of the Set, will first use the hashCode value of the object.

According hashCode it first to determine whether the object is not equal, as long as it is not equal, HashSet would think they certainly are not the same object, you can save the call overhead equals.

If two objects are equal equals, hashCode but not equal, it would lead to think that HashSet is not equal two objects, two objects will be added to the HashSet, may cause abnormal program.

 

Guess you like

Origin www.cnblogs.com/dingpeng9055/p/11065433.html