Object类可以称之为java中最重要的一个类。java是单根继承的语言,这是和c++的区别之一。在jdk1.8源码中的Object类中的方法有以下这些,有很多方法事实上源码的注释已经解释的非常清楚了,所以我直接把有些注释粘贴了出来
(1)
private static native void registerNatives();
static {
registerNatives();
}
(2)
返回当前这个Object运行时的class对象
public final native Class<?> getClass();
(3)
返回当前对象的hash code的值,这个方法是为了用于支持hash表比如HashMap
return a hash code value for this object
public native int hashCode();
(4)
Indicates whether some other object is "equal to" this one
表示某一个对象与这个对象是否一样.
需要指出的是,这里的一样是指的是否是同一个对象。
public boolean equals(Object obj) {
return (this == obj);
}
(5)
返回一个对象的一个克隆体
return a clone of this instance。
The precise meaning
* of "copy" may depend on the class of the object. The general
* intent is that, for any object {@code x}, the expression:
* <blockquote>
* <pre>
* x.clone() != x</pre></blockquote>
* will be true, and that the expression:
* <blockquote>
* <pre>
* x.clone().getClass() == x.getClass()</pre></blockquote>
* will be {@code true}, but these are not absolute requirements.
* While it is typically the case that:
* <blockquote>
* <pre>
* x.clone().equals(x)</pre></blockquote>
* will be {@code true}, this is not an absolute requirement.
protected native Object clone() throws CloneNotSupportedException;
(6)
返回一个用于表达一个对象的字符串。
return a string representation of the object.
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
(7)
Wakes up a single thread that is waiting on this object's
* monitor. If any threads are waiting on this object, one of them
* is chosen to be awakened. The choice is arbitrary and occurs at
* the discretion of the implementation. A thread waits on an object's
* monitor by calling one of the {@code wait} methods.
public final native void notify();
(8)
Wakes up all threads that are waiting on this object's monitor. A
* thread waits on an object's monitor by calling one of the
* {@code wait} methods
public final native void notifyAll();
(9)
/**
* Causes the current thread to wait until either another thread invokes the
* {@link java.lang.Object#notify()} method or the
* {@link java.lang.Object#notifyAll()} method for this object, or a
* specified amount of time has elapsed.
* <p>
* The current thread must own this object's monitor.
public final native void wait(long timeout) throws InterruptedException;
(10)
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);
}
(11)
/**
* Causes the current thread to wait until another thread invokes the
* {@link java.lang.Object#notify()} method or the
* {@link java.lang.Object#notifyAll()} method for this object.
* In other words, this method behaves exactly as if it simply
* performs the call {@code wait(0)}.
public final void wait() throws InterruptedException {
wait(0);
}
(12)
由垃圾收集器(garbage collector)判定一个对象没有引用(reference)的时候对一个对象调用。子类可以通过重写这个方法来处理系统资源,或者来执行清理操作
protected void finalize() throws Throwable { }