java object类原码详解

  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 的常规协定是:  

  15. 1.在 Java 应用程序执行期间,在对同一对象多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是将对象进行 equals 比较时所用的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。   

  16. 2.如果根据 equals(Object) 方法,两个对象是相等的,那么对这两个对象中的每个对象调用 hashCode 方法都必须生成相同的整数结果。   

  17. 3.如果根据 equals(java.lang.Object) 方法,两个对象不相等,那么对这两个对象中的任一对象上调用 hashCode 方法不 要求一定生成不同的整数结果。但是,程序员应该意识到,为不相等的对象生成不同整数结果可以提高哈希表的性能。  

  18. */  

  19.   

  20.     public native int hashCode();   

  21.   

  22.   

  23.     public boolean equals(Object obj) {   

  24.     return (this == obj);   

  25.     }   

  26.   

  27.     /*本地CLONE方法,用于对象的复制。*/  

  28.     protected native Object clone() throws CloneNotSupportedException;   

  29.   

  30.     /*返回该对象的字符串表示。非常重要的方法*/  

  31.     public String toString() {   

  32.     return getClass().getName() + "@" + Integer.toHexString(hashCode());   

  33.     }   

  34.   

  35.    /*唤醒在此对象监视器上等待的单个线程。*/  

  36.     public final native void notify();   

  37.   

  38.    /*唤醒在此对象监视器上等待的所有线程。*/  

  39.     public final native void notifyAll();   

  40.   

  41.   

  42. /*在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待。换句话说,此方法的行为就好像它仅执行 wait(0) 调用一样。   

  43. 当前线程必须拥有此对象监视器。该线程发布对此监视器的所有权并等待,直到其他线程通过调用 notify 方法,或 notifyAll 方法通知在此对象的监视器上等待的线程醒来。然后该线程将等到重新获得对监视器的所有权后才能继续执行。*/  

  44.     public final void wait() throws InterruptedException {   

  45.     wait(0);   

  46.     }   

  47.   

  48.   

  49.   

  50.    /*在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过指定的时间量前,导致当前线程等待。*/  

  51.     public final native void wait(long timeout) throws InterruptedException;   

  52.   

  53.     /* 在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者其他某个线程中断当前线程,或者已超过某个实际时间量前,导致当前线程等待。*/  

  54.     public final void wait(long timeout, int nanos) throws InterruptedException {   

  55.         if (timeout < 0) {   

  56.             throw new IllegalArgumentException("timeout value is negative");   

  57.         }   

  58.   

  59.         if (nanos < 0 || nanos > 999999) {   

  60.             throw new IllegalArgumentException(   

  61.                 "nanosecond timeout value out of range");   

  62.         }   

  63.   

  64.     if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {   

  65.         timeout++;   

  66.     }   

  67.   

  68.     wait(timeout);   

  69.     }   

  70.   

  71.     /*当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。*/  

  72.     protected void finalize() throws Throwable { }   

  73. }  

protected Object clone()创建并返回此对象的一个副本。 
boolean equals(Object obj)指示其他某个对象是否与此对象“相等”。 
protected void finalize()当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。 
Class<?> getClass()返回此 Object 的运行时类。 
int hashCode()返回该对象的哈希码值。 
void notify()唤醒在此对象监视器上等待的单个线程。 
void notifyAll()唤醒在此对象监视器上等待的所有线程。 
String toString()返回该对象的字符串表示。 
void wait()在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待。 
void wait(long timeout)在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过指定的时间量前,导致当前线程等待。 
void wait(long timeout, int nanos)在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者其他某个线程中断当前线程,或者已超过某个实际时间量前,导致当前线程等待。

转载于:https://my.oschina.net/stayStand/blog/632157

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值