以下系列笔记都是基于jdk1.8
首先看下Object类的源码,其方法如下:
private static native void registerNatives();
static {
registerNatives();
}
public final native Class<?> getClass();
public native int hashCode();
protected native Object clone() throws CloneNotSupportedException;
public final native void notify();
public final native void notifyAll();
public final native void wait(long timeout) throws InterruptedException;
protected void finalize() throws Throwable { }
public boolean equals(Object obj) {
return (this == obj);
}
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
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);
}
【2】本地方法notify()、notifyall()、wait(long timeout)与多线程有关
【3】equals() 方法判断对象相等
【4】本地方法hashCode() 表示对象的地址,每次执行都不一定相等
如果equals相等,则hashCode必定相等;反之则不一定
如果重写了类的equals,必须要重写hashCode;反之亦然
【5】保护方法finalize() 与GC有关