Java Learning Note 13

Source: Internet
Author: User

The object class is the parent class of all classes, arrays, and enumeration classes, and is the root class of the class hierarchy. Each class uses object as a superclass. All objects, including


arrays) Implement this class of methods.


The object class implements the following methods:


Let's take a look at the source code in the Hashcode () method:

public native int hashcode ();


Object.hashcode () is a hash function that computes the hash value to implement the data structure of the hash table.

Returns the hash code value of the object. This method is supported to improve the performance of a hash table (for example, java.util.Hashtable a hash table provided).


hashCodeThe General agreement is:

1. During Java application execution, when calling the hashcode method multiple times on the same object, the same integer must be returned consistently, provided is a


the object is equals the information used in the comparison has not been modified. From one execution of an application to another execution of the same application,


The integer does not need to be consistent.


2, if according to equals (Object) method, two objects are equal, and each object in the two objects is called hashCode methods must be born


Into the same integer result.


3, if according to equals(java.lang.Object) method, two objects are not equal, then a call to any of these two objects hashcode


Method No requires that a different integer result must be generated. However, programmers should be aware that generating different integer results for unequal objects can improve


The performance of the hash table.


In fact, the Hashcode method defined by the object class does return different integers for different objects. (This is typically done by adding the object


's internal address is translated into an integer, but the JavaTM programming language does not require this implementation technique. )


In real-world development, The Equals () method is overridden in addition to the Hascode () method.


Let's take a look at the Equals () method in the source code:

public boolean equals (Object obj) {        return (this = = obj);    }
we can see from the above procedure that the Equals () method is compared to an object and is equal.


equalsmethod to implement an equality relationship on a non-null object reference:

1. reflexive Nature : For any non-null reference value x , x.equals(x) should all return true .


2. symmetry of : For any non-null reference value x and the y , when and only if y.equals(x) return true when the x.equals(y) should not return true .


3. transitivity : For any non-null reference value x , y and the z , if x.equals(y) return true , and y.equals(z) return true , thenx.equals(z)


should return true .


4. Consistency : For any non-null reference value x and the y , multiple calls x.equals (y) always return true or always returns false , provided that the object


equalsThe information used in the comparison has not been modified.


5. For any non-null reference value x , x.equals(null) should all return false .


ObjectThe equals method of a class implements the most differentiated equality relationship on an object, that is, for any non-null reference value x and y , if and only ifx


yThis method returns true ( x == y with a value) when the same object is referenced true .


Note: When this method is overridden, it is often necessary to override the Hashcode method to maintain the general contract of the hashcode method, which declares equality


The object must have an equal hash code.


See the following procedure for how to override the Equals () and Hashcode () methods:

Class User {    private String name;        Protected String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }    @Override public    boolean equals (Object obj) {        if (obj = = NULL | |! ( obj instanceof User) {            return false;        } else{        return true;        }    }    @Override public    int hashcode () {        return;    }} public class Main {public static void main (string[] args) {User U1 = new User (); U1.setname ("GU");   User U2 = new user (); U2.setname ("Bill"); System.out.println (U1.equals (U2));}}

Output Result:

True

In the above program, the Equals () method and the Hashcode () method are overridden in the user class.




Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/43899101 Emotional Control _  

Java Learning Note 13

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.