JDK源码阅读之Object

 

package java.lang;

/**
 * {@link Object}对象是类结构层次的根,每个类都会以Object类作为父类(就算是没有指定也会默认Object为父类)。包括数组在内,都会实现Object的所有方法。
 * @author  unascribed
 * @see     java.lang.Class
 * @since   JDK1.0
 */

/**
 * Object对象是类结构层次的根,每个类都会以Object类作为父类(就算是没有指定也会默认Object为父类)。包括数组在内,都会实现Object的所有方法。
 *
 */
public class Object {

    /**
     * 调用本地方法初始化
     */
    private static native void registerNatives();
    static {
        registerNatives();
    }

    /**
     * 此方法返回Object的运行时对象,也就是Class对象。返回的Class对象是被类的静态同步方法加锁的对象。
     *
     * 实际类型为Class<? extends |X|>
     * @return The {@code Class} object that represents the runtime
     *         class of this object.
     * @jls 15.8.2 Class Literals
     */
    public final native Class<?> getClass();

    /**
     * 返回对象的哈希值。
     * 一般约定是:
     * 在执行java应用的一次执行在同一个对象上多次调用此方法时,hashcode必须返回相同的整数,
     * 前提是不修改equals在比较时会使用到的属性信息。但是如果是两次不同的执行,不许要保持一致。
     *
     * 如果调用equals方法两个对象相等,则两个对象调用hashCode方法必须生成相同的hash值。
     * 如果调用equals方法两个对象不想等,则两个对象调用hashCode方法必须生成不相同的hash值。
     * 为不相等的对象生成不同的hash值可以调高hash表的性能(如果能完全做到这一条,hash表就可以做到O(1)级别的查询)
     *
     * 一般情况下,Objec对象的hashCode方法确实为不同的对象返回不同的hash值,通常是通过将对象的内部地址转换为hash值。
     *
     * @return  a hash code value for this object.
     * @see     java.lang.Object#equals(java.lang.Object)
     * @see     java.lang.System#identityHashCode
     */
    public native int hashCode();

    /**
     * 判断此对象是否和另一对象相等。
     * 对于任何非空对象引用:
     * x.equals(x)应该返回true,即自反性。
     * 如果x.equals(y)返回true,那么y.equals(x)也返回true,即对称性。
     * 如果x.equals(y)返回true,且y.equals(z)返回true,那么x.equals(z)返回true,即传递性。
     * 如果x.equals(y)返回他人use,那么多次调用都应该一致的返回true,即一致性。
     * x.equals(null)应该返回falsa。
     * 如果x和y指定同一对象,那么 x==y 应该返回true,Objet的默认实现就是比较引用是否相同。
     * 一旦覆盖此方法必须覆盖hashCode方法。一遍维护hashcode方法的约定,该方法比较相等的对象hash值必须相等。
     * @param   obj   the reference object with which to compare.
     * @return  {@code true} if this object is the same as the obj
     *          argument; {@code false} otherwise.
     * @see     #hashCode()
     * @see     java.util.HashMap
     */
    public boolean equals(Object obj) {
        return (this == obj);
    }

    /**
     * 创建并返回此对象副本。
     * x.clone()!=x 为 true
     * x.clone().getClass() == x.getClass() 为 true
     * x.clone().equals(x) 为 true
     * 但是上面的条件不是强制要求的。
     *
     * 按照惯例,返回的对象应该是通过调用super.clone() 获得,
     * 如果一个类及其父类能遵守这个约定,那么就能满足 x.clone().getClass() == x.getClass()。
     *
     * 按照惯例,此对象返回的克隆对象应该独立于此对象。要实现独立性,可能需要在返回之前修改
     * super.clone()返回的对象的一个或多个字段。。通常这意味着复制包含被克隆对象内部“深层
     * 结构”的任何可变对象,并使用副本的引用替换这些对象的引用。如果一个对象内部只包含原始
     * 字段或者不可变对象的引用,那么不需要修改super.clone()对象中的任何字段都不需要修改。
     *
     * Object的clone()方法执行特定的克隆操作。首先,如果此对象未实现Cloneable接口,则抛出
     * CloneNotSupportException。这里注意,所有数组都是被人为实现了接口Cloneable,其中数组
     * 类型T[]的clone()方法的返回类型是T[]。否则,此方法创建的此对象的新实例,并使用该对象
     * 的相应字段的内容分初始化其所有字段,就像通过赋值一样,这些字段的内容本身并不会被克
     * 隆。因此Object的clone()方法是“浅克隆”,而不是“深克隆”。Object对象并未实现
     * Cloneable接口,所以Object对象的实例调用clone()方法会直接抛出异常。
     * @return     a clone of this instance.
     * @throws  CloneNotSupportedException  if the object's class does not
     *               support the {@code Cloneable} interface. Subclasses
     *               that override the {@code clone} method can also
     *               throw this exception to indicate that an instance cannot
     *               be cloned.
     * @see java.lang.Cloneable
     */
    protected native Object clone() throws CloneNotSupportedException;

    /**
     * 返回对象的文本表示的字符串。
     * 建议所有子类都应该重写此方法。Object类的toString()方法返回字符串的格式为:对象实例的类
     * 的名字+"@"+无符号十六进制表示的对象哈希值。
     * @return  a string representation of the object.
     */
    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

    /**
     * 唤醒在锁对象上等待的单个线程。唤醒的线程可以参与竞争锁,不过只有持有当前线程的锁的对
     * 象放弃锁才能获得。
     * 这个方法只能由持有锁的线程调用,线程可以通过三种途径持有锁:
     * (1)执行该对象的同步实例方法
     * (2)执行synchronized同步的代码块。
     * (3)执行该类的同步静态方法
     * @throws  IllegalMonitorStateException  if the current thread is not
     *               the owner of this object's monitor.
     * @see        java.lang.Object#notifyAll()
     * @see        java.lang.Object#wait()
     */
    public final native void notify();

    /**
     * 唤醒在锁对象上等待的所有线程。 线程通过调用{@code wait}方法之一等待对象的监视器。
     * 其他特点和notify()方法一样。
     * @throws  IllegalMonitorStateException  if the current thread is not
     *               the owner of this object's monitor.
     * @see        java.lang.Object#notify()
     * @see        java.lang.Object#wait()
     */
    public final native void notifyAll();

    /**
     * 导致当前线程等待,直到另一个线程调用notify()、notifyAll()或者指定的等待时间已过。
     * 此方法将当前线程放入等待集合中,在以下四种情况之一发生以前线程会一直处于等待状态:
     * (1)其他线程调用此对象的notify()方法,并且此线程恰巧被选择作为被唤醒线程。
     * (2)其他线程调用此对象的notifyAll()方法。
     * (3)其他线程通过interrupt()方法中断线程。
     * (4)指定的得时间已经过了。如果timeout指定为0,则不考虑这一条,线程只能等待被唤醒。
     * 被唤醒后,首先从等待集合中删除此线程,重新开始线程调度。然后此线程一普通的方式继
     * 续竞争锁对象,一旦获得锁对象就可以恢复到原来同的同步状态。
     *
     * 线程也可以在没有被通知、中断或超时的情况下被唤醒。即所谓的虚假唤醒(实际上很少发
     * 生)最好是将wait()方法放入while循环中,如下:
     *     synchronized (obj) {
     *         while (&lt;condition does not hold&gt;)
     *             obj.wait(timeout);
     *         ... // Perform action appropriate to condition
     *     }
     *
     *     注意:和Thread的sleep()方法做区别,它会放弃对锁对象的控制权,sleep()方法不会。
     * @param      timeout   the maximum time to wait in milliseconds.
     * @throws  IllegalArgumentException      if the value of timeout is
     *               negative.
     * @throws  IllegalMonitorStateException  if the current thread is not
     *               the owner of the object's monitor.
     * @throws  InterruptedException if any thread interrupted the
     *             current thread before or while the current thread
     *             was waiting for a notification.  The <i>interrupted
     *             status</i> of the current thread is cleared when
     *             this exception is thrown.
     * @see        java.lang.Object#notify()
     * @see        java.lang.Object#notifyAll()
     */
    public final native void wait(long timeout) throws InterruptedException;

    /**
     * 超时等待,和wait()方法的区别就在于可以指定等待时间。
     *
     * @param      timeout   the maximum time to wait in milliseconds.
     * @param      nanos      additional time, in nanoseconds range
     *                       0-999999.
     * @throws  IllegalArgumentException      if the value of timeout is
     *                      negative or the value of nanos is
     *                      not in the range 0-999999.
     * @throws  IllegalMonitorStateException  if the current thread is not
     *               the owner of this object's monitor.
     * @throws  InterruptedException if any thread interrupted the
     *             current thread before or while the current thread
     *             was waiting for a notification.  The <i>interrupted
     *             status</i> of the current thread is cleared when
     *             this exception is thrown.
     */
    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");
        }

        //只要纳秒超过0,就直接让等待的毫秒数加一
        if (nanos > 0) {
            timeout++;
        }

        wait(timeout);
    }

    /**
     * 当前线程等待。
     * @throws  IllegalMonitorStateException  if the current thread is not
     *               the owner of the object's monitor.
     * @throws  InterruptedException if any thread interrupted the
     *             current thread before or while the current thread
     *             was waiting for a notification.  The <i>interrupted
     *             status</i> of the current thread is cleared when
     *             this exception is thrown.
     * @see        java.lang.Object#notify()
     * @see        java.lang.Object#notifyAll()
     */
    public final void wait() throws InterruptedException {
        wait(0);
    }

    /**
     * 进行垃圾之前会调用此方法。
     * 如果finalize方法抛出未捕获的异常,则忽略该异常并终止该对象的终止。
     * 调用finalize时,调用finalize()方法的线程不会持有任何用户可见的同步锁。
     * 在为对象调用finalize()方法之后,在Java虚拟机再次确定此对象尚未死亡之前,不会清除
     * 此对象。对于任何对象,虚拟机只调用一次finalize()方法。finalize()方法抛出异常会导
     * @throws Throwable the {@code Exception} raised by this method
     * @see java.lang.ref.WeakReference
     * @see java.lang.ref.PhantomReference
     * @jls 12.6 Finalization of Class Instances
     */
    protected void finalize() throws Throwable { }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值