JDK源码阅读--Object

在java.lang包下
Object类:是全部类的基类(父类)
public final native Class<?> getClass();
返回这个Object所表明的的运行时类

public native int hashCode();//返回对象hash code
若是是两个相同(指用=比较都为true的)的对象调用这个方法,会返回相同的Integer类型的hash码;
若是是两个不相同(指用=比较都为false的)的对象调用这个方法,会返回不一样的Integer类型的hash码;

/**
* The {@code equals} method for class {@code Object} implements
* the most discriminating possible equivalence relation on objects;
* that is, for any non-null reference values {@code x} and
* {@code y}, this method returns {@code true} if and only
* if {@code x} and {@code y} refer to the same object
* ({@code x == y} has the value {@code true}).
* <p>
*/
public boolean equals(Object obj) {
return (this == obj);
}
返回值表示两个对象是否"相同".true表示"相同",false表示"不相同"
x.equals(x) 返回true
非空引用x,y:
若是x.equals(y) 返回true,那么y.equals(x) 也返回true
具备传递性:
非空引用x,y,z:
若是 x.equals(y) 返回true, y.equals(z) 返回true,那么x.equals(z) 也返回true
若是x非空:
x.equals(null) 返回false
参数obj表明要与之比较的对象,也就是要被比较de对象。
当equals方法被重写的时候,必须也要重写hashCode方法,确保拥有相同的hash码。


protected native Object clone() throws CloneNotSupportedException;
返回一个复制的对象(注意浅复制、深复制)。
调用这个方法的对象所在的类须要实现Cloneable接口才能使用到此方法,不然会抛出CloneNotSupportedException异常。


public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
返回对象字符串的表现形式


public final native void notify();
唤醒单个线程,可是唤醒的线程不会立马继续执行,直到持有锁的线程释放这个锁,被唤醒的线程将和其余线程一同竞争去得到这个锁。


public final native void notifyAll();
唤醒全部线程,可是唤醒的线程不会立马继续执行,直到持有锁的线程释放这个锁,被唤醒的线程将和其余线程一同竞争去得到这个锁。

public final native void wait(long timeout) throws InterruptedException;
timeout的单位为 毫秒。
让线程进入等待,直到另外一个线程调用notify()或notifyAll()方法或者超过了设置的时间timeout 才会中止等待。
当前线程必须拥有对象的监视器(锁)
若是当前线程在等待以前或等待期间被任何线程中断,则会抛出InterruptedException异常。
若是当前线程不是对象监视器所拥有的,则抛出IllegalMonitorStateException异常。
若是timeout是负数,则抛出IllegalArgumentException异常。


/*@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");
}

if (nanos > 0) {
timeout++;
}

wait(timeout);
}
nanos:额外的时间,以纳秒为单位,范围在0-999999.
让线程进入等待,直到另外一个线程调用notify()或notifyAll()方法或者超过了设置的时间timeout 才会中止等待。
timeout的单位为 毫秒。
让线程进入等待,直到另外一个线程调用notify()或notifyAll()方法或者超过了设置的时间timeout 才会中止等待。
当前线程必须拥有对象的监视器(锁)
若是当前线程在等待以前或等待期间被任何线程中断,则会抛出InterruptedException异常。
若是当前线程不是对象监视器所拥有的,则抛出IllegalMonitorStateException异常。
若是timeout是负数,则抛出IllegalArgumentException异常。
若是nanos范围越界,则抛出IllegalArgumentException异常。


public final void wait() throws InterruptedException {
wait(0);
}
这个方法就像在调用wait(0);
让线程进入等待,直到另外一个线程调用notify()或notifyAll()方法或者超过了设置的时间timeout 才会中止等待。

让线程进入等待,直到另外一个线程调用notify()或notifyAll()方法或者超过了设置的时间timeout 才会中止等待。
当前线程必须拥有对象的监视器(锁)
若是当前线程在等待以前或等待期间被任何线程中断,则会抛出InterruptedException异常。
若是当前线程不是对象监视器所拥有的,则抛出IllegalMonitorStateException异常。


protected void finalize() throws Throwable { }
当肯定再也不有对该对象的引用时,垃圾回收时由对象上的垃圾回收器调用