您的位置:首页 > 移动开发 > Objective-C

java学习笔记(1)-Object类

2017-08-07 15:59 267 查看
以下系列笔记都是基于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);
}

【1】注册本地方法:registerNatives()    静态块中调用
【2】本地方法notify()、notifyall()、wait(long timeout)与多线程有关

【3】equals()  方法判断对象相等

【4】本地方法hashCode()  表示对象的地址,每次执行都不一定相等

  如果equals相等,则hashCode必定相等;反之则不一定

  如果重写了类的equals,必须要重写hashCode;反之亦然

【5】保护方法finalize()  与GC有关
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java jdk源码