Object類源碼分析

Object是java所有類的父類(超類),1.0版本以來就有

這個類公有12個方法,去除重載有10個

private static native void registerNatives();
static {
    registerNatives();
}

native 關鍵字表示調用本地方法

靜態代碼塊在程序啓動加載時就初始化

public final native Class<?> getClass();

返回運行時的類對象

public native int hashCode();

調用本地哈希方法,獲得哈希值

public boolean equals(Object obj) {
    return (this == obj);
}

判斷對象值,如果子類重寫了equals,必須重寫hashCode方法

protected native Object clone() throws CloneNotSupportedException;

 克隆對象,該方法是淺複製,受保護的方法,如果子類想複製,需要實現了Cloneable接口否則拋出異常

public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
public final native void notify();

 喚醒在該對象上等待的某個線程

public final native void notifyAll();

 喚醒在該對象上等待的所有線程

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

 線程等待

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 > 0) {
            timeout++;
        }

        wait(timeout);
    }
public final void wait() throws InterruptedException {
        wait(0);
    }

 調用wait(long timeout)

protected void finalize() throws Throwable { }

該方法受保護,用於垃圾回收,釋放資源

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章