Object 类研究

Object 类研究

概述

java中Object是所有对象的父类,上帝类。里面的方法我们应该对其深入掌握。


Object方法展示

代码展示:

public class Object {

    private static native void registerNatives();
    static {
        registerNatives();
    }

    //获得类对应的Class对象
    public final native Class<?> getClass();

    //hashCode码,如不覆盖就调用操作系统本地的hashCode方法。
    public native int hashCode();

    //比较是否相等,默认直接比较是否引用同一个地址。
    public boolean equals(Object obj) {
        return (this == obj);
    }

    protected native Object clone() throws CloneNotSupportedException;

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


    /**
     * Called by the garbage collector on an object when garbage collection
     * determines that there are no more references to the object.
     * A subclass overrides the  finalize method to dispose of
     * system resources or to perform other cleanup.
     */
    //主动调用GC去回收改对象在jvm堆栈的垃圾
    protected void finalize() throws Throwable { }


    //以下几个方法只有在同步时获得对象锁时才可以调用,否则会报异常。

    //唤醒一个线程
    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 {
        //省略入参校验
        wait(timeout);
    } 
    public final void wait() throws InterruptedException {
        wait(0);//就是无限期sleep除非其它线程唤醒它。
    }
}

说明:注意哪些方法是final修饰不可被覆盖的,哪些方法是调用本地方法的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值