Object 源码

在源码中可以看到该类是在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( )

返回的是根据当前物理地址生成的哈希码,区分不同的对象

  1. 两个对象通过equals 比较返回 true , 则hashCode也应该相同。
  2. 两个对象通过equals比较返回false , 则hashCode可以相等,也可以不等。

3,equals()

public boolean equals(Object obj) {
        return (this == obj);
    }

比较两个对象是否相等。
该方法重写的时候需要注意满足几个条件

  1. x!=null ==> x.equals(x)
  2. 对称性
  3. 传递性
  4. null 与任何对象不等

4,clone()方法

如果当前对象的类没有实现Cloneable接口,抛CloneNotSupportedException异常

5,toString() 方法

public String toString() {  
    return getClass().getName() + "@" + Integer.toHexString(hashCode());  
}  

该对象的类名 + “@” + 该对象 hashCode 的16进制表示形式

wait() notify() 则是线程释放锁,进入等待 以及唤醒线程的方法。

参考:http://www.jiancool.com/article/36483070621/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值