java学习笔记(1)-Object类

来源:互联网 发布:淘宝打单插件 编辑:程序博客网 时间:2024/06/09 19:29

以下系列笔记都是基于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有关




原创粉丝点击