Introduction
Object class: This class java.lang.java is the parent class inherited by all classes by default.
Three commonly used methods in the Object class: toString() , equal() , hashCode()
1. Self-description of the toString() method object and self-introduction of the object
During the self-description of an object, since it is troublesome to print instance variables using the get() method, toString() is used for simplicity.
Public String toString(){Return "Learn well" + getNo() + "Name" + getName();} System.out.println(s); is equivalent to System.out.println(s.toString());
Printing:
a. Print its value directly when the basic data type
b. Call toString() when referring to the data type and print
If toString() method in the Object class is not rewrite, print the object directly, and the result obtained in the console is:
HashCode containing full name @ object
2. Rewriting equals() method equals() must rewrite hashCode().
Equals() compares the contents of two objects, or the two objects; hashCode() is the internal address of the object and is an integer.
==Difference between equals()?
Answer: ==The essential comparison is binary, the basic data type compares numerical values, and the reference data type compares address
Equals() is divided into equals() of the Object class and equals() of the String class. equals() of the Object class is equivalent to == comparing the reference type; equals() of the String class overrides equals() method in the Object class, comparing literal values.
Instanceof determines the class name, or may use reflection
The object's reference variable name instanceof class name expression, if the object's reference variable name is the class or base class object whose return value is still true.
Custom equals() method:
1. The same type
2. Define your own comparison rules
public boolean equals(Object object){ //Rewrite the equals() method of Object EqualsTest equalsTest = (EqualsTest) object;if(equalsTest.getClass() == EqualsTest.class){ //??????????????if((this.name==equalsTest.name)&&(this.price==qualsTest.price)){return true;}else{return false;}}else{return false;}}3. The hashCode() method is used to accelerate search and implement index search (query algorithm, storage algorithm)
Public int hashCode(){Return this.name.hashCode()*13 + new Interger(getAge).hashCode()*12;}Two principles of hashCode:
1. When the return value of the two objects equals() methods is true, their hashCode() methods must also ensure that the return value is the same.
2. Use equals() to compare the properties used in the object, and all of them should be used to calculate hashCode() .
hashCode() hash algorithm
need:
1. The return value of hashCode() is the same, and the return value equals() must also be the same.
2. Rewrite what attributes to use hashCode() and what attributes to use in equals() method.
Note: hashCode is the same, equals is not necessarily the same.
hashCode is used to judge folders, and equals is used to judge files.
If hashCode() method is not overridden, the default hashCode() method is found based on the object's address (the key value is the object's address)
If you rewrite hashCode() method, it cannot be used to determine whether it is the same object.
You can use System.identityHashCode (the reference name of the object) If the hashCode is the same, it is the same object.
System.out.println(System.identityHashCode(对象的引用名));
If there is no hashCode() , the hashCode of its object is calculated by the object address.
System.out.println(对象引用名.hashCode());
Note: Whether the elements exist in the HashSet collection and the methods for deleting operation dependencies are hashCode() and equals() of the element.
Summarize
The above are three common methods of parent Object in Java compiled for you. I hope it will be helpful to you. If you have any questions, you can leave a message to communicate. Thank you for your support for Wulin.com.