== and equals and hashCode

== and equals

==: its role is to determine the target address is not equal to two. That is, it is determined the objects are not the same object (primitive data type is a value == comparison, the comparison reference data type == memory address).
equals (): its role is to determine whether two objects are equal. It is generally used in two cases:
Case 1: No cover class equals () method. Through equals () comparing two objects of the class, it is equivalent to the "==" to compare the two objects.
Case 2: class overrides equals () method. Generally, we cover the equals () method to compare the contents of two objects are equal; if their contents are equal, returns true (that is, the two objects are considered equal).

==

In fact, for ==, you do not have to distinguish between basic data types, or a reference type, value comparisons are stored objects only, but should be noted that: java, the basic data type is the actual stored value, such as whole specific value type; and the variable reference to an object, is stored in the address space of the stack.
Here particular attention to the type of string, because special treatment java string, a string containing double quotes is stored in a string constant pool, when a plurality of string reference point to the string variable, in fact, directed is the same memory area, returns true == inevitable;
however, if it is out of the new string object, at this time point to the reference variable is the specific object in the heap address space, the address space if another variable to another reference point ( For example, the string address pool scene), must be returned == false.

For strings, also noted the operator "+" is performed, two string objects + operator, will first two string values ​​are summed and then find the string constant pool, if it already exists, then the character directly Normally string object back pool; string if the pool is not common sense, it will create a new string constant pool string object, and returns. In this way during the == operator, you need to figure out individual objects, it may cause an error of judgment.

In addition, in fact, appropriate caution should be! = Time, we must pay attention to.

equals

Requires two cases:
If the object does not implement the equals method, is used to judge ==;
if they achieve the object equals method, the return value is obtained according to this particular method.
Note that here the return value is entirely determined by the equals method does not fully address associated with two objects and content, for example, if the equals method returns true or false, then the two objects are the same whether or not an object, or the contents are equal, it will not bring different results equals. Of course, in general, equals the method object of the java package implemented, or are more content addresses. For example, equals a string object methods:

public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        } else {
            if (anObject instanceof String) {
                String aString = (String)anObject;
                if (this.coder() == aString.coder()) {
                    return this.isLatin1() ? StringLatin1.equals(this.value, aString.value) : StringUTF16.equals(this.value, aString.value);
                }
            }

            return false;
        }
    }

== and equals

In fact, the two are not associated with the original, just equals in the implementation might use ==, but because of our habits, often determine whether two things are equal, == will be used directly, when the two when an object, and we expect the results will likely be inconsistent. And this is also more prone to error, and no aid to prompt us.
Therefore, A better approach is possible:
1. All equals determined by two things are equal (decision object can be used when == null)
2 and the like can not appear in the code 0, 1 or hard-coded character
3. You do not directly use the basic data types used package type

hashCode

hashCode () describes
hashCode () functions to acquire the hash code, also called a hash code; it actually returns an int integer. The role of this hash code is to determine the location of the object in the hash index table. hashCode () is defined in the JDK Object.java, this means that any Java class contains a hashCode () function.
Hash table is stored in key-value pair (key-value), which is characterized by: to "value" in accordance with "key" corresponding to the retrieved quickly. This one on the use of the hash code! (You can quickly find the object needed)

hashCode与equals

hashCode () and equals () the relevant provisions

  • If two objects are equal, it must also be the same hashcode
  • Equal two objects, two objects were to call the equals method returns true
  • Hashcode two objects have the same value, they are not necessarily equal
  • Thus, it equals overridden the method, the method must be covered hashCode
  • The default behavior hashCode () is to create a unique value to objects on the heap. If no override hashCode (), then the two objects are not equal class anyway (even if these two objects point to the same data)

The first two objects are equal, hashCode not necessarily the same, such as equals rewritten;
second, nonsense, does not make sense;
the third, in fact, is nonsense, this is simply because the relationship between the two and we have to discuss not a cent of the association, this article can be completely do not care about (this one, of course, the actual hash table when the application is the application);

note! There is a characteristic of the hash table is a hash table can not have more than one object is equal, that the calling object of equals can not be equal.
There is a characteristic of the hash table is a hash table can not have more than one object is equal, that the calling object of equals can not be equal. (This is the core!)

However, for efficiency, it leads to the concept of a hashCode.
When inserted into the hash table, first calls the object hashCode calculated hash value
; 1. If the value is not repeated, the hash table is inserted directly
equal to 2. If the repeat value, and then determines whether the call object equals object is not equal then insert the hash table, otherwise it will still be inserted into the hash table

If you override the equals method of the class, it may lead to two different native hashCode results of the algorithm object into the hash table, but this time, there are two objects may Equals, leading to contradictions:
So, in relation when the hash table, if you override the Equals class, you must override hashCode class to avoid, when two objects Equals, hashCode is different, that is able to guarantee, two objects are equal (Equals), it does not are repeatedly inserted into the hash table (specifically how rewrite until the subsequent study).

On the other hand, if the hash table is not related to the corresponding class, with not a cent Equals relationship with hashCode, both can be completely rewrite it or not do not care

Published an original article · won praise 0 · Views 31

Guess you like

Origin blog.csdn.net/weixin_44521516/article/details/104628469