Object类源码详解(基于jdk1.8.0_261)

1. Object 简介

  • Object是所有类的超类,包括Arrays类都实现了Object类的方法;

2. Object UML

3. Object 源码

package java.lang;
public class Object {
    private static native void registerNatives();
    static {
        registerNatives();
    }
    public final native Class<?> getClass();
    public native int hashCode();
    public boolean equals(Object obj) {
        return (this == obj);
    }
    protected native Object clone() throws CloneNotSupportedException;
    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
    public final native void notify(); //通知第一个wait或最后一个wait的线程继续运行
    public final native void notifyAll(); //通知所有wait的线程依次(可能顺序可能逆序)继续运行
    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(0)主要是为了让线程放锁,让其他线程有机会得到锁 
    }
    protected void finalize() throws Throwable { }
}

4. Object使用示例



posted @ 2020-07-17 02:45  ahpuched  阅读(366)  评论(0编辑  收藏  举报