在源码中可以看到该类是在java.lang 包中。
首先看Object中的方法:
public final native Class<?> getClass();
public native int hashCode();
public boolean equals(Object obj);
protected native Object clone() throws CloneNotSupportedException;
public String toString();
public final native void notify();
public final native void notifyAll();
public final native void wait(long timeout) throws InterruptedException;
public final void wait(long timeout, int nanos) throws InterruptedException;
public final void wait() throws InterruptedException;
protected void finalize() throws Throwable;
很多naive 方法,这些方法不是用Java实现的,而是c/c++ 实现的。通过本地方法接口调用的本地方法。
1,getClass()
返回此Object 运行时的类。
2,hashCode( )
返回的是根据当前物理地址生成的哈希码,区分不同的对象
- 两个对象通过equals 比较返回 true , 则hashCode也应该相同。
- 两个对象通过equals比较返回false , 则hashCode可以相等,也可以不等。
3,equals()
public boolean equals(Object obj) {
return (this == obj);
}
比较两个对象是否相等。
该方法重写的时候需要注意满足几个条件
- x!=null ==> x.equals(x)
- 对称性
- 传递性
- null 与任何对象不等
4,clone()方法
如果当前对象的类没有实现Cloneable接口,抛CloneNotSupportedException异常
5,toString() 方法
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
该对象的类名 + “@” + 该对象 hashCode 的16进制表示形式
wait() notify() 则是线程释放锁,进入等待 以及唤醒线程的方法。