环境:JDK 1.7 Eclipse
Object 类源码如下
1 package java.lang; 2 3 public class Object { 4 5 private static native void registerNatives(); 6 static { 7 registerNatives(); 8 } 9 10 public final native Class<?> getClass(); 11 12 public native int hashCode(); 13 14 public boolean equals(Object obj) { 15 return (this == obj); 16 } 17 18 protected native Object clone() throws CloneNotSupportedException; 19 20 public String toString() { 21 return getClass().getName() + "@" + Integer.toHexString(hashCode()); 22 } 23 24 public final native void notify(); 25 26 public final native void notifyAll(); 27 28 public final native void wait(long timeout) throws InterruptedException; 29 30 public final void wait(long timeout, int nanos) throws InterruptedException { 31 if (timeout < 0) { 32 throw new IllegalArgumentException("timeout value is negative"); 33 } 34 35 if (nanos < 0 || nanos > 999999) { 36 throw new IllegalArgumentException( 37 "nanosecond timeout value out of range"); 38 } 39 40 if (nanos >= 500000 || (nanos != 0 && timeout == 0)) { 41 timeout++; 42 } 43 44 wait(timeout); 45 } 46 47 public final void wait() throws InterruptedException { 48 wait(0); 49 } 50 51 protected void finalize() throws Throwable { } 52 }
Object 类中方法及说明如下:
1 registerNatives() //私有方法 2 getClass() //返回此 Object 的运行类。 3 hashCode() //用于获取对象的哈希值。 4 equals(Object obj) //用于确认两个对象是否“相同”。 5 clone() //创建并返回此对象的一个副本。 6 toString() //返回该对象的字符串表示。 7 notify() //唤醒在此对象监视器上等待的单个线程。 8 notifyAll() //唤醒在此对象监视器上等待的所有线程。 9 wait(long timeout) //在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或 者超过指定的时间量前,导致当前线程等待。 10 wait(long timeout, int nanos) //在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者其他某个线程中断当前线程,或者已超过某个实际时间量前,导致当前线程等待。 11 wait() //用于让当前线程失去操作权限,当前线程进入等待序列 12 finalize() //当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。 13
说明:
native
java关键字,Native Method 用以修饰非 java 代码实现的方法(C || C++), 类似java调用非java代码的接口。