1. 程式人生 > >java原始碼分析——java.lang.Object

java原始碼分析——java.lang.Object

所有的java類均繼承Object類,

package java.lang;


public class Object {
    public Object() {
    }

    private static native void registerNatives();

    public final native Class<?> getClass();

    public native int hashCode();

    /*比較兩個物件是否相等,實際上比較的是兩個物件的引用*/
    public boolean equals(Object var1) {
        return this == var1;
    }

    /*克隆*/
    protected native Object clone() throws CloneNotSupportedException;

    public String toString() {
        return this.getClass().getName() + "@" + Integer.toHexString(this.hashCode());
    }

    public final native void notify();

    public final native void notifyAll();

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


    /*執行緒等待,實際上傳入了納秒後毫秒就自增1,精度實際還是納秒*/
    public final void wait(long var1, int var3) throws InterruptedException {
        if (var1 < 0L) {
            throw new IllegalArgumentException("timeout value is negative");
        } else if (var3 >= 0 && var3 <= 999999) {
            if (var3 > 0) {
                ++var1;
            }

            this.wait(var1);
        } else {
            throw new IllegalArgumentException("nanosecond timeout value out of range");
        }
    }

    public final void wait() throws InterruptedException {
        this.wait(0L);
    }
    /*垃圾回收前要做的操作,一般來說都會重寫這個方法*/
    protected void finalize() throws Throwable {
    }

    static {
        registerNatives();
    }
}