JDK8源码阅读-Object类

概述

Object是全部类的基类,属于java.lang包。java

构造方法

只有编译器提供的默认构造方法。微信

字段

Object类中没有成员字段。多线程

方法

Object类一共12个方法。this

Object类Structure

按照访问等级分spa

  • public: getClass()、hashCode()、equals(Object obj)、toString()、notify()、notifyAll()、wait(long timeout)、wait(long timeout, int nanos)、wait()
  • protected: clone()、finalize()
  • private: registerNatives()

按照是否为final分线程

  • final:getClass()、notify()、notifyAll()、wait(long timeout)、wait(long timeout, int nanos)、wait()
  • 非final:registerNatives()、hashCode()、equals(Object obj)、clone()、toString()、finalize()

按照是否为native分3d

  • native:registerNatives()、getClass()、hashCode()、clone()、notify()、notifyAll()、wait(long timeout)
  • 非native:equals(Object obj)、toString()、wait(long timeout, int nanos)、wait()、finalize()

registerNatives()


private static native void registerNatives();
    static {
        registerNatives();
    }
复制代码

在初始化调用static代码块时调用,用于注册Object类中的静态方法。code

getClass()


public final native Class<?> getClass();
复制代码

返回类对象的Class实例,synchronized静态代码块时锁定的正是Class实例对象。cdn

hashCode()、equals(Object obj)


public native int hashCode();

public boolean equals(Object obj) {
    return (this == obj);
}
复制代码

hashCode()返回一个带符号int,返回一个对象实例的hashCode,其值多是对象的存储地址;equals方法用来判断两个对象是否相等。对象

Java规范中对hashCode、equals方法有以下规范:

  1. hashCode相等,equals不必定为true
  2. equal方法为true,hashCode必定相等
  3. hashCode不想等,equals必定不为true
  4. equals方法为false,hashCode可能相等

重写equals(Object obj)方法时必须同时重写hashCode()方法

clone()


protected native Object clone() throws CloneNotSupportedException;
复制代码
  • 用于对java对象进行复制,可分为深复制和浅复制。
  • 若是对象未实现Colneable接口,会抛出CloneNotSupportedException异常

toString()


public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
复制代码

默认实现为Class名+"@"+十六进制的hashCode

多线程相关notify()、notifyAll()、wait(long timeout)、wait(long timeout, int nanos)、wait()


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);
}
复制代码
  • notify():唤醒一个正在等待这个对象的monitor线程,若是有多个线程在等待,只能唤醒其中一个
  • notifyAll():唤醒全部在等待这个对象的monitor线程
  • wait(long timeout):将当前持有此对象锁的线程进入等待状态,直到线程被notify()、notifyAll()、Thread.interrupts()方法唤醒或者超时。
    • 若是timeout为0,则会一直等待,直到被另外唤醒
  • wait(long timeout, int nanos):与wait(long timeout)相似,但添加了一个nanos参数以提供更细粒度的等待时间控制
  • wait():默认当前持有对象锁的线程进入等待状态并一直等待直到被notify()、notifyAll()、Thread.interrupts()方法唤醒

finalize()


protected void finalize() throws Throwable { }
复制代码

在GC回收对象实例时会被调用,因GC是不肯定且随机的,没法肯定此方法的执行时间(若是内存充足,GC永远都不会发生则finalize()方法一直都不会被调用)。


欢迎扫码关注我微信公众号:magicTan。

相关文章
相关标签/搜索