Java的Object类的9大方法

  • getClass
//获得该对象的类型类
public final native Class<?> getClass();
  • hashCode
//返回对象存储的物理地址
public native int hashCode();
  • equals
//默认的方法是比较对象的引用是否在同一地址
//重写以后可以比较两个个对象的值是否相同
public boolean equals(Object obj) {
    return (this == obj);
}
  • clone
//快速的创建一个对象的副本,并且两个对象指向不同的内存地址。
protected native Object clone() throws CloneNotSupportedException;
  • toString
//输出一个对象的哈希code码,地址的字符串
public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
  • notify
//随机选择一个在该对象上调用wait方法的线程,解除其阻塞状态。
public final native void notify();
  • notifyAll
//解除所有那些在该对象上调用wait方法的线程的阻塞状态
public final native void notifyAll();
  • finalize
//垃圾回收器准备释放内存的时候,会先调用finalize()。因为无法确定该方法什么时候被调用,很少使用。
protected void finalize() throws Throwable { }
  • wait
//1
//导致线程进入等待状态,直到它被其他线程通过notify()或者notifyAll唤醒
public final void wait() throws InterruptedException {
    wait(0);
}
//2
//进入等待,经过timeout 超时后,若未被唤醒,则自动唤醒
public final native void wait(long timeout) throws InterruptedException;
//3
//进入等待,经过timeout 超时后,若未被唤醒,则自动唤醒。相对wait(long timeout) 更加精确时间。
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);
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值