解释: 1.Object类是所有Java类的祖先。每个类都使用 Object 作为超类。所有对象(包括数组)都实现这个类的方法。 2.可以使用类型为Object的变量指向任意类型的对象。 Java Object类的详细内容
package java.lang; public class Object { //native修饰,说明这是一个本地方法,具体是用C(C++)在DLL中实现的,然后通过JNI调用。 private static native void registerNatives(); //此处是对象初始化的时候调用 static { registerNatives(); } //native方法,final修饰的,返回运行时对象的类 //注释翻译:表示此对象的运行时类的@code class对象。 public final native Class<?> getClass(); //native方法,返回对象的哈希代码值 public native int hashCode(); //比较是否是同一对象。 //必须引用和存储的内存完全一致才返回true,否则false; //String重写了object中的equals方法,对比的是字符串内容(内存)。 public boolean equals(Object obj) { return (this == obj); } //native方法,克隆,涉及浅copy和深copy protected native Object clone() throws CloneNotSupportedException; // public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } //唤醒在该对象上等待的某个线程 public final native void notify(); //唤醒在该对象上等待的所有线程 public final native void notifyAll(); //native方法,使当前线程等待指定的时间,直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法,或者超过指定的时间量。 //当前的线程必须拥有此对象监视器。 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调用前都必须持有锁,然后调用该方法会释放锁。 public final void wait() throws InterruptedException { wait(0); } //重写finalize()方法,在该对象垃圾回收前被调用,不建议使用 protected void finalize() throws Throwable { } }方法不是很多,但是里面有一堆问题和技术点可以问。是个不错的面试开端问题。
下次整理;