java object默認的基本方法

java object默認的基本方法中沒有copy(),含有以下方法:
getClass(), hashCode(), equals(), clone(), toString(), notify(), notifyAll(),  wait(), finalize()java

 

拿出來源碼對比一下方法:程序員

  1. package  java.lang;   
  2. public   class  Object {   
  3.     
  4.     /* 一個本地方法,具體是用C(C++)在DLL中實現的,而後經過JNI調用。*/     
  5.      private   static   native   void  registerNatives();   
  6.    /* 對象初始化時自動調用此方法*/   
  7.      static  {   
  8.         registerNatives();   
  9.     }   
  10.     /* 返回此 Object 的運行時類。*/   
  11.      public   final   native  Class<?> getClass();   
  12.   
  13. /*   
  14. hashCode 的常規協定是:(本質 上是 返回該對象的哈希碼值。 )
  1.    
  2. 1.在 Java 應用程序執行期間,在對同一對象屢次調用 hashCode 方法時,必須一致地返回相同的整數,前提是將對象進行 equals 比較時所用的信息沒有被修改。從某一應用程序的一次執行到同一應用程序的另外一次執行,該整數無需保持一致。    
  3. 2.若是根據 equals(Object) 方法,兩個對象是相等的,那麼對這兩個對象中的每一個對象調用 hashCode 方法都必須生成相同的整數結果。    
  4. 3.若是根據 equals(java.lang.Object) 方法,兩個對象不相等,那麼對這兩個對象中的任一對象上調用 hashCode 方法不 要求必定生成不一樣的整數結果。可是,程序員應該意識到,爲不相等的對象生成不一樣整數結果能夠提升哈希表的性能。   
  5. */   
  6.   
  7.      public   native   int  hashCode();   
  8.   
  9.   
  10.      public   boolean  equals(Object obj) {   
  11.      return  ( this  == obj);   
  12.     }   
  13.   
  14.      /*本地CLONE方法,用於對象的複製。*/   
  15.      protected   native  Object clone()  throws  CloneNotSupportedException;   
  16.   
  17.      /*返回該對象的字符串表示。很是重要的方法*/   
  18.      public  String toString() {   
  19.      return  getClass().getName() +  "@"  + Integer.toHexString(hashCode());   
  20.     }   
  21.   
  22.     /*喚醒在此對象監視器上等待的單個線程。*/   
  23.      public   final   native   void  notify();   
  24.   
  25.     /*喚醒在此對象監視器上等待的全部線程。*/   
  26.      public   final   native   void  notifyAll();   
  27.   
  28.   
  29. /*在其餘線程調用此對象的 notify() 方法或 notifyAll() 方法前,致使當前線程等待。換句話說,此方法的行爲就好像它僅執行 wait(0) 調用同樣。    
  30. 當前線程必須擁有此對象監視器。該線程發佈對此監視器的全部權並等待,直到其餘線程經過調用 notify 方法,或 notifyAll 方法通知在此對象的監視器上等待的線程醒來。而後該線程將等到從新得到對監視器的全部權後才能繼續執行。*/   
  31.      public   final   void  wait()  throws  InterruptedException {   
  32.     wait( 0 );   
  33.     }   
  34.   
  35.   
  36.   
  37.     /*在其餘線程調用此對象的 notify() 方法或 notifyAll() 方法,或者超過指定的時間量前,致使當前線程等待。*/   
  38.      public   final   native   void  wait( long  timeout)  throws  InterruptedException;   
  39.   
  40.      /* 在其餘線程調用此對象的 notify() 方法或 notifyAll() 方法,或者其餘某個線程中斷當前線程,或者已超過某個實際時間量前,致使當前線程等待。*/   
  41.      public   final   void  wait( long  timeout,  int  nanos)  throws  InterruptedException {   
  42.          if  (timeout <  0 ) {   
  43.              throw   new  IllegalArgumentException( "timeout value is negative" );   
  44.         }   
  45.   
  46.          if  (nanos <  0  || nanos >  999999 ) {   
  47.              throw   new  IllegalArgumentException(   
  48.                  "nanosecond timeout value out of range" );   
  49.         }   
  50.   
  51.      if  (nanos >=  500000  || (nanos !=  0  && timeout ==  0 )) {   
  52.         timeout++;   
  53.     }   
  54.   
  55.     wait(timeout);   
  56.     }   
  57.   
  58.      /*當垃圾回收器肯定不存在對該對象的更多引用時,由對象的垃圾回收器調用此方法。*/   
  59.      protected   void  finalize()  throws  Throwable { }   
  60. }
相關文章
相關標籤/搜索