What are the basic methods of Object

1. clone method

The protection method implements the shallow copy of the object. This method can only be called if the Cloneable interface is implemented, otherwise a CloneNotSupportedException is thrown.

2. getClass method

final method to get the runtime type.

3. toString method

This method is used more often and is generally covered by subclasses.

4. finalize method

This method is used to release resources. Rarely used because there is no way to determine when this method is called.

5. equals method

This method is a very important one. Generally equals and == are not the same, but in Object they are the same. Subclasses generally override this method.

6. hashCode method

This method is used for hash lookup. Overriding the equals method generally requires overriding the hashCode method. This method is used in some Collections with hash function.

Generally, obj1.equals(obj2)==true must be satisfied. It can be deduced that obj1.hash-Code()==obj2.hashCode(), but the equals of hashCode does not necessarily satisfy equals. However, in order to improve efficiency, we should try to make the above two conditions as close to equivalent as possible.

7. wait method

The wait method is to make the current thread wait for the lock of the object. The current thread must be the owner of the object, that is, it has the lock of the object. The wait() method waits until the lock is acquired or interrupted. wait(long timeout) sets a timeout interval and returns if the lock is not acquired within the specified time.

After calling this method, the current thread goes to sleep until the following event occurs.

(1) Other threads call the notify method of the object.

(2) Other threads call the notifyAll method of the object.

(3) Other threads call interrupt to interrupt the thread.

(4) The time interval is up.

At this point the thread can be scheduled, and if it is interrupted, an InterruptedException is thrown.

8. notify method

This method wakes up a thread waiting on this object.

9. notifyAll method

This method wakes up all threads waiting on this object.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326059387&siteId=291194637