Object class analysis

xiaoxiao2021-03-06  62

Object is a parent class of all classes, and any class inherits Object by default.

Here to see what the Object class provides.

1.clone method

Protection method, realize the shallow copy of the object, only the cloneable interface can be called, otherwise the CloneNotSupportedException is thrown, and the reference cloneable interface analysis:

http://blog.9cbs.net/treeroot/archive/2004/09/07/96936.aspx

2.GetClass method

Final method, get runtime type

3.Tostring method

There are many more, the general child has coverage

4.Finalize method

Release the resources, because when you are not determined, when is the method being called, rarely use

5.Equals method

Very important way, general equals and == are different, but both in Object are the same, and subclasses generally rewrite this method.

6.hashcode method

Used for hash search, rewriting the equals method usually to rewrite the HashCode method, this method is used in some collections with hash function.

It is generally necessary to satisfy: obj1.equals (obj2) == True can launch obj1.hashcode () == obj2.hashcode ()

But HashCode does not necessarily meet Equals, but in order to improve efficiency, we should try to close the above two conditions.

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, the lock with the object. The Wait () method is waiting until the lock or is interrupted.

Wait (long timeout) is set to have a time division, and if you do not get a lock in the specified time, you will return.

When the method is called, the current thread enters the sleep state until the following event happens:

1) Other threads call the NOTIFY method of the object

2) Other threads call the NotifyAll method for this object

3) Other threads call the interrupt interrupt that thread

4) time interval

The thread can be scheduled, and an interruptedException exception is thrown if it is interrupted.

8.Notify method

This method wakes up a certain thread waiting for this object.

9.NotifyAll method

This method wakes up all threads waiting for this object.

转载请注明原文地址:https://www.9cbs.com/read-119355.html

New Post(0)