object class methods commonly used

 

Object class method

Object is the parent class of all classes, default inherit any class Object. Object class in the end to achieve what method?

(1) clone Method

Protection methods to achieve the object of a shallow copy, only to achieve the Cloneable interface can call this method, otherwise throw a CloneNotSupportedException.

(2) getClass method

final method, to obtain run-time type.

(3) toString Method

The process uses more than generally subclass has cover.

(4) finalize method

The method for releasing resources. Because it can not determine when the method is called, it is rarely used.

(5) equals Method

This method is a very important method. Generally equals and == is not the same, but in Object two are the same. Subclass generally required to override this method.

(6) hashCode method

The method used to hash lookup, rewrite the equals method generally required to override the hashCode method. This method is used in some of the Collection with a hash function.

General must meet obj1.equals (obj2) == true. You can launch obj1.hash- Code () == obj2.hashCode (), but does not necessarily equal hashCode meet equals. However, in order to improve efficiency, we should try to make the above two conditions are nearly equivalent.

(7) wait method

wait is to make the current thread wait for a lock of the object, the current thread must be the owner of the object, that is, has a lock on the object. wait () method waits until the lock is obtained or is interrupted. wait (long timeout) setting a timeout interval, if a lock is not obtained within a predetermined time period returns.

After calling this method the current thread to sleep until the following events occur.

(1) other thread calls the notify method of the object.

(2) other thread calls notifyAll method of the object.

(3) other thread calls interrupt interrupt this thread.

(4) the time interval to.

At this time, the thread can be scheduled, if it is interrupted, then throw an InterruptedException exception.

(8) notify method

The method awaken a thread waiting on the object.

(9) notifyAll method

This method wakes up all threads waiting on the object.

Guess you like

Origin www.cnblogs.com/4756yaoyexingjun/p/12003801.html