01 java.lang.Object

本文详细解读Java中Object类的基本属性与方法,包括getClass、hashCode、clone、equals、toString、finalize等,并讨论其在Map、Set等数据结构中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Object

                                                            2015.01.14
                                                            By 970655147

/ *
哎哟, 卧槽, 不得不吐槽一句, 这csdn的markDown编辑器, 能在我切换到其他播客的时候, 帮我保存之前的内容到草稿箱一下么, 这逗比编辑器, 好了, 这下又要返工重写了
* /

相信学过java的朋友, 一定都知道java.lang.Object, 因为他是面向对象的基石呀, 然而对于这个类, 并没有多少需要说的, 因为其中除了equals, toString, finalize 方法, 其余的方法均是本地方法[或者依赖于本地方法], 但是对于这个类的一些方法, 我们还是需要有一些映像的

start ->

声明

, 大家可以看看注释

/**
 * Class {@code Object} is the root of the class hierarchy.
 * Every class has {@code Object} as a superclass. All objects,
 * including arrays, implement the methods of this class.
 *
 * @author  unascribed
 * @see     java.lang.Class
 * @since   JDK1.0
 */
public class Object

Object. getClass()/ hashCode()/ clone()

    // 获取Class对象
    public final native Class<?> getClass();

    // hashCode  native实现
    public native int hashCode();

    // clone  本地实现
    protected native Object clone() throws CloneNotSupportedException;

Object. equals(Object obj)

    // equals  ==判定, 在我们使用Map, Set 类数据结构的时候, 根据数据结构的性质, 我们可能需要重写equals, hashCode 方法
    public boolean equals(Object obj) {
        return (this == obj);
    }

Object. toString()

    // toString  打印出类名+@+hashCode
// 这就是我们通常打印一个对象, 看到的是 "类名 + 七七八八的数字"的原因
    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }

Object.finalize()

    // gc回收对象空间 时会调用的方法, 默认实现不做任何事情
    protected void finalize() throws Throwable { }

Object. notify()/ notifyAll()/ wait(long timeout)

    // 多线程相关的三个方法
    // 根据算法notify一个线程
    public final native void notify();

    // notifyAll  notify所有wait的线程
    public final native void notifyAll();

    // wait(timeout) 本地实现
    public final native void wait(long timeout) throws InterruptedException; 

Object. wait(long timeout, int nanos)/ wait()

// 依赖于wait的两个wait重载方法
 Object. wait(long timeout, int nanos)
    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");
        }
        // 四舍五入nanos 如果timeout==0 timeout++
        if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
            timeout++;
        }

        wait(timeout);
}


    Object.wait()
    public final void wait() throws InterruptedException {
        wait(0);
}

-> end

Object 相关的东西也不多, 但是挺重要的, 有一个了解即可, 因为在后来, 你会见到他的地方越来越多, 看多了, 便自然会记住一些东西了


资源下载 : http://download.csdn.net/detail/u011039332/9025839

注 : 因为作者的水平有限,必然可能出现一些bug, 所以请大家指出!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值