The Methods of class Object

来源:互联网 发布:thread0806.php fid 7 编辑:程序博客网 时间:2024/04/28 14:17

近日稍感空闲,温习一下基础知识,时刻准备着,等待7.27的到来。


Class Object is the root of the class hierarchy , Every class has Object as a superest class. All objects, including arrays, implement the methods of this class.

1. getClass
public final Class <? extends Object > getClass()
Returns the runtime class of the object.


2. hashCode

public int hashCode()


Return a hashCode value for the object. This method is supported for the benifit of hashtables such as those provided by the Hashtables.
The general contract of hashCode is :
1) When ever it is invoked on the same object more than once during the execution of a Java application, the hashCode method must consistantly return the same integer, provided no information used in equals comparision on the object is modified. This integer need not remian consistant from one execution of an application to another execution of the same.
2)If two objects are equal according to the method equals(Object), then calling the hashCode methond on each of the two objects must produce the same integer result.
3)It is not required that if two objects are unequal according to the method equals(Object), then calling the hashCode methond on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of the hashtables.
As much as is reasonably practical, the hashCode method defined by Object does return distinct integers for distinct objects.(This is typically implemented by converting internal address of an object into an integer, but this implementation technique is not required by Java programming language).


3. equal
public boolean equal(Object obj)
Indicates whether some other object is equal to this one.
The equals method implements the most discriminating possiable equivalence relation on objects, that is , for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object(x==y has the value true).
Note that it is generally necessary to override the hashCode method whenever this method is overridden , so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hashCodes.


4.clone
protected Object clone() throws CloneNotSupportedException

Creates and returns a copy of this object. This precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression: 
x.clone()!=x  will be true, and that the expression x.clone.getClass()==x.getClass will be true, but these are not absolute requirements. While it is typically the case that:
x.clone.equals(x), this is not an absolute requirement.

By convention ,  the returned object should be independent of this object( which is being cloned).  --Shallow Copy vs Deep Copy
To achieve this independence, it may be necessary to modify one or more fields of the object by calling super.clone()  before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies.  If a class only contains primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone() need to be modified.

super.clone()  method  performs a "shallow copy" of this object instead of a "deep copy" operation. This method creates a new instance of the class of this object and initialize all its fields with exactly the contents of of the corresponding fields of this object , as if by assignment. The contents of the fields are not themselves cloned.

5. toString
  public String toString()


  Returns a string representation of the object. In general, the toString method returns a string that "textually represents" the object.  The result should be a concise but informative representation that is easy for a person to read. It is recomended that all subclasses override this method.

  This method returns a string equal to the value of :  getClass().getName()+"@"+Integer.toHexString(hashCode())

6.notify
  public void final notify()
 
  Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object's montor, one of them is chosen to be awakened. A thread waits on an object's montor by calling one of the wait method.
  
  Only one thread at a time can own one object's montor.


  The awakened method will not be able to proceed until the current thread relinquishes the lock on the object. The awakened method will compete in the usual manner with any other threads that might be actively competing to synchronize on this object. For example, the awakened thread has no reliable priviledge or disadvantage in being the next thread to lock this object.

  This method should only be called bya thread that is the owner of the object's monitor. A thread becomes the owner of the object's monitor in one of the three ways:
  1)By executing a synchronized instance method of that object
  2)By executing the body of a synchronized statement that synchronizes on the object. 
  3)For objects of type Class, by executing a synchronized static methods of that class.

7. notifyAll
   public void final notifyAll()
    
   The difference between the method notifyAll  and notify is that notifyAll wakes up all threads that are waiting on this object's monitor. 
   
8.wait
public void final wait(long timeout) throws InterruptedException

Causes current thread to wait until either another thread invokes the notify()  method or notifyAll() method for this object, or a specified amount of time has elapsed.

The current thread must own the object's montior.

This method causes current thread(call it T) to place itself  in the wait set for this object, and then to relinquish any and all synchornization claims on this object. Thread T becomes disabled for thread scheduling purpose and lies dormant until one of four things happens.
1)Some other thread invokes the notify method for this object, and thread T happens to be arbitrarily chosen as the thread to be awakened.
2)Some other thread invokes the notifyAll method for this object.
3)Some other thread interrupts thread T.
4)The specified amount of real time has elapsed, more or less. However, if timeout is zero, the real time is not taken into consideration and the thread simply waits until notified.


A thread can also wake up without being notified, interrupted or timing out, a so-called spurious wakeup(虚假唤醒).???

Note that the wait method , as it places the current thread into the wait set for this object, unlocks only this object, any other objects on which   current thread may be synchornized remain locked.

9. wait
public void final wait(long timeout,long nanos)throws InterruptedException


与wait(long timeout)基本一样,不同之处在于时间更加精确,支持毫微秒


10.wait
public void final wait()throws InterruptedException

同wait()一样

11.finalize
protected void finalize() throws Throwable

Called by the Garbage Collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.


No GC Roots------F-Queue----- call finalize if overrided.


If an uncaught exception is thrown by the method finalize,  the exception is ignored and the finalization of that object terminates.

原创粉丝点击