网络知识 娱乐 Java 基础 | Object 源码解

Java 基础 | Object 源码解

Java 是一门面向对象的语言,在 Java 里面一切都可以看作是一个对象,而 Java 里面所有的对象都默认继承于 Object 类,所以狗哥今天就从源码角度复习了一遍这个类。

上图看出 Object 一共有 12 个方法,其中 registerNatives() 是由 C 语言实现的,这个不在研究范围内。

1、getClass

/**
     * Returns the runtime class of this {@code Object}. The returned
     * {@code Class} object is the object that is locked by {@code
     * static synchronized} methods of the represented class.
*/
public final native Class<?> getClass();

这个方法的作用就是返回某个对象的运行时类,它的返回值是 Class 类型,Class c = obj.getClass();通过对象 c ,我们可以获取该对象的所有成员方法,每个成员方法都是一个 Method 对象;我们也可以获取该对象的所有成员变量,每个成员变量都是一个 Field 对象;同样的,我们也可以获取该对象的构造函数,构造函数则是一个 Constructor 对象。这个方法在反射时会常用到。

2、hashCode

/**
     * Returns a hash code value for the object. This method is
     * supported for the benefit of hash tables such as those provided by
     * {@link java.util.HashMap}.
*/
    public native int hashCode();

这个方法的注释比较长,就不放出来了。注释指出:

  • hashCode 方法返回散列值。
  • 返回值默认是由对象的地址转换而来的。
  • 同一个对象调用 hashCode 的返回值是相等的。
  • 两个对象的 equals 相等,那 hashCode 一定相等。
  • 两个对象的 equals 不相等,那 hashCode 也不一定相等。

3、equals

public boolean equals(Object obj) {
    return (this == obj);
}

equals 的实现非常简单,它的作用就是比较两个对象是否相等,而比较的依据就是二者的内存地址。除此之外,equals 还遵循以下几个原则:

1、自反性:x.equals(x);  // true
2、对称性:x.equals(y) == y.equals(x);  // true
3、传递性:if (x.equals(y) && y.equals(z))
            x.equals(z); // true;

4、一致性,只要对象没有被修改,多次调用 equals() 方法结果不变:
x.equals(y) == x.equals(y); // true 

5、非空性,对任何不是 null 的对象 x 调用 x.equals(null) 结果都为 false :
x.equals(null); // false;

为什么要重写 hashcode 和 equals ?

这个问题之前分享过旧文:为什么要重写 equals 和 hashCode 方法

4、clone

protected native Object clone() throws CloneNotSupportedException;

clone() 是 Object 的 protected 方法,它不是 public,一个类不显式去重写 clone(),其它类就不能直接去调用该类实例的 clone() 方法。此外,Clone 的注释中还提到比较重要的几点:

  • 克隆的对象必须要实现 Cloneable 接口并重写 clone 方法,否则会报 CloneNotSupportedException 异常
  • clone() 方法并不是 Cloneable 接口的方法,而是 Object 的一个 protected 方法。Cloneable 接口只是规定,如果一个类没有实现 Cloneable 接口又调用了 clone() 方法,就会抛出 CloneNotSupportedException。
  • 浅拷贝:拷贝对象和原始对象的引用类型引用同一个对象。
  • 深拷贝:拷贝对象和原始对象的引用类型引用不同对象。

关于浅拷贝与深拷贝的详解,请看这篇旧文:Java 深拷贝与浅拷贝

5、toString

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

这个方法应该没什么好讲的,原生的 toString 方法仅仅返回,对象名 + 它的 hashCode ,但做过开发的都知道,原生的 toString 作用不大。我们需要重写 toString 一般是因为方便调试,需要知道对象的属性值,而不仅仅是 hashCode 。所以,应该像下面这样重写:

public class Student {

    private int age;

    private String name;

    // 省略 get、set

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + ''' +
                '}';
    }
}

6、notify 和 wait

public final native void notify();
public final native void notifyAll();

首先是 notify ,注释就不贴出来了,notify 的作用就是随机唤醒在等待队列的某个线程,而 notifyAll 就是唤醒在等待队列的所有线程。

public final void wait() throws InterruptedException {
     wait(0);
}

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);
}

然后是 wait ,wait 的作用是让当前线程进入等待状态,同时,wait() 也会让当前线程释放它所持有的锁。直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法,当前线程被唤醒进入就绪状态。

wait(long timeout) (以毫秒为单位)让当前线程处于等待(阻塞)状态,直到其他线程调用此对象的notify() 方法或 notifyAll() 方法,或者超过指定的时间量,当前线程被唤醒进入就绪状态。

wait(long timeout, int nanos) 和 wait(long timeout) 功能一样,唯一的区别是这个可以提供更高的精度。总超时时间(以纳秒为单位)计算为 1000000 *timeout+ nanos。By the way ,wait(0,0) 和 wait(0) 效果一样。

除此之外,notify 和 wait 的注释中还有这么一段:

* <p>
     * This method should only be called by a thread that is the owner
     * of this object's monitor. A thread becomes the owner of the
     * object's monitor in one of three ways:
     * <ul>
     * <li>By executing a synchronized instance method of that object.
     * <li>By executing the body of a {@code synchronized} statement
     *     that synchronizes on the object.
     * <li>For objects of type {@code Class,} by executing a
     *     synchronized static method of that class.
     * </ul>
* <p>

看到这英文,刚过四级的我瑟瑟发抖。以上注释主要就是描述了,notify 和 wait 方法的使用规范。意思就是这二者必须在 synchronized 修饰的同步方法或同步代码中使用。

(1) 为什么 wait() 必须在同步 (Synchronized) 方法/代码块中调用?

答:调用 wait() 就是释放锁,释放锁的前提是必须要先获得锁,先获得锁才能释放锁。

(2) 为什么 notify()、notifyAll() 必须在同步 (Synchronized) 方法/代码块中调用?

答:notify()、notifyAll() 是将锁交给含有 wait() 方法的线程,让其继续执行下去,如果自身没有锁,怎么叫把锁交给其他线程呢?(本质是让处于入口队列的线程竞争锁)

(3) Thread.sleep() 和 Object.wait() 有什么区别?

首先,二者都可以暂停当前线程,释放 CPU 控制权。主要的区别在于 Object.wait()在释放 CPU 同时,释放了对象锁的控制。而 Thread.sleep() 没有对锁释放。换句话说 sleep 就是耍流氓,占着茅坑不拉屎。

详细解释请参考以下博文:

https://blog.csdn.net/qq_42145871/article/details/81950949