# Object源碼閱讀 > native:本地棧方法,使用C語言中實現的方法。 ```java package java.lang; public class Object { //註冊本地方法 private static native void registerNatives(); stati ...
Object源碼閱讀
native:本地棧方法,使用C語言中實現的方法。
package java.lang;
public class Object {
//註冊本地方法
private static native void registerNatives();
static {
registerNatives();
}
//返回Object對象的class
public final native Class<?> getClass();
//根據Object對象的記憶體地址計算hash值
public native int hashCode();
//比較兩個對象記憶體地址是否相同
public boolean equals(Object obj) {
return (this == obj);
}
//淺拷貝:指針的拷貝,指針指向同一記憶體地址,如:Object obj1 = obj;
//深拷貝:對象的拷貝
//對象拷貝,深拷貝(記憶體地址不同)
protected native Object clone() throws CloneNotSupportedException;
//返回類名+@對象記憶體地址求hash再轉16進位
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
//喚醒一個等待object的monitor對象鎖的線程
public final native void notify();
//喚醒所有等待object的monitor對象鎖的線程
public final native void notifyAll();
//讓當前線程處於等待(阻塞)狀態,直到其他線程調用此對象的 notify() 方法或 notifyAll() 方法,或者超過參數 timeout 設置的超時時間
public final native void wait(long timeout) throws InterruptedException;
//nanos是額外時間,超時時間為timeout + nanos
public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
timeout++;
}
wait(timeout);
}
//方法讓當前線程進入等待狀態。直到其他線程調用此對象的 notify() 方法或 notifyAll() 方法
public final void wait() throws InterruptedException {
wait(0);
}
//GC確定不存在對該對象的更多引用,回收時會調用該方法
protected void finalize() throws Throwable { }
}