以往一些Java程序员面试中,很多面试官为了考量一个面试者 的 Java基础掌握程度和基本的源码关注度,会问到这个问题,下面我就简单的记录一下这个类,以供参考:
首先Object类里面有八个native修饰的方法
public final native Class<?> getClass();
public native int hashCode();
protected native Object clone() throws CloneNotSupportedException;
public final native void notify();
public final native void notifyAll();
public final native void wait(long timeout) throws InterruptedException;
在这几个方法中比较常用的是getClass,hashCode,clone,notify,notifyAll方法,关于这些方法的作用,在后续的博文中体现。
除此之外,该类中还有几个更为常用的方法如下:
public boolean equals(Object obj) {
return (this == obj);//比较相等方法,一般提供给子类重写
}
` public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());//转换为字符串方法,一般为子类重写
}`
protected void finalize() throws Throwable { }`//系统方法,控制Java虚拟机方法,一般很少使用。
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);
}
“`
总的来说就上面几个方法,一般能回答以上的9成以上为佳