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 { }

该方法受保护,用于垃圾回收,释放资源

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