一、Object 类
类层次结构的根。关键想粗略了解它有哪些基本方法。
package java.lang;
public class Object {
private static native void registerNatives();
static {
registerNatives();
}
public final native Class<?> getClass();/*返回对象的运行时的类,即该对象实际的类型*/
public native int hashCode();/*将返回一个对象的哈希码值,典型的实现方法是把该对象的内部地址转换为一个整形值,所以每个对象的此值具有唯一性*/
public boolean equals(Object obj) {/*注释中有一句:无论何时重载此方法,有必要同时重载hashCode(),这是为了维护equals相同的对象,其哈希码值也相同*/
return (this == obj);
}
protected native Object clone() throws CloneNotSupportedException;
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());/*toHexString()即转换为16进制字符串。*/
}
public final native void notify();/*唤醒在此对象上具有同步操作(Synchronized)的线程,被唤醒的每个线程都不具有可靠的权限与优势来争取操作此对象*/
public final native void notifyAll();/*唤醒其他所有等待的线程*/
/*注:能调用notify与notifyAll,则当前线程必须是该Synchronized变量或方法的拥有者,怎么才能成为拥有者呢?三种方法:1、执行对象的Synchronized实例方法;2、执行Synchronized 语句块;3、对于Class 类型的对象,执行sychronized 静态方法。简单理解,只有在synchronized修饰的方法或语句块中可使用notify与notifyAll*/
public final native void wait(long timeout) throws InterruptedException;/*线程进入等待状态,等待被notify或notifyAll*/
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 >= 500000 || (nanos != 0 && timeout == 0)) {
timeout++;
}
wait(timeout);
}
public final void wait() throws InterruptedException {
wait(0);
}
protected void finalize() throws Throwable { }
}pack