码迷,mamicode.com
首页 > 编程语言 > 详细

Java-Object源码分析

时间:2015-08-29 06:23:36      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:object

java.lang包在使用的时候无需显示导入,编译时由编译器自动导入。Object类是类层次结果的根,Java中所有的类从根本上都继承自这个类。Object类是Java中唯一没有父类的子类。其他所有的类,包括标准容器类,比如数组,都继承了Object类中的方法。
Object类包括的方法有:
(1)Object():默认的构造函数。
(2)getClass():返回一个对象的运行时类。
(3)hashCode():返回给对象的一个哈希码值。
(4)equals(Object obj):判断某个对象obj是否与此对象相等。
(5)clone():创建并返回此对象的一个副本。
(6)toString():返回由类名和由hashCode值转换为16进制的字符串。
(7)notify():唤醒在此对象监视器上等待的单个线程。
(8)notifyAll():唤醒在此对象监视器上等待的所有线程。
(9)wait():导致当前的线程等待,直到其他线程调用此对象的notify()方法或者notifyAll()方法。
(10)wait(long timeout)导致当前的线程等待,直到其他线程调用此对象的notify()方法或者notify(),或者超过指定的时间量。
(11)wait(long timeout,int nanos):导致当前的线程等待,直到其他线程调用此对象的notify()方法或者notifyAll()方法,或者其他某个线程中断当前线程,或者已经超过某个实际时间量。
(12)registerNatives():静态方法,对几个本地方法进行注册,以上几个函数有些是本地方法的。

基于Java8的Object源码:

public class Object {

    private static native void registerNatives();
    static {
        registerNatives();
    }
    public final native Class<?> getClass();
    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());
    }
    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 {
        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 >= 500000 || (nanos != 0 && timeout == 0)) {
            timeout++;
        }

        wait(timeout);
    }
    public final void wait() throws InterruptedException {
        wait(0);
    }
    protected void finalize() throws Throwable { }
}

版权声明:知识在于分享,技术在于交流,转载时请留一个博主的链接就好

Java-Object源码分析

标签:object

原文地址:http://blog.csdn.net/qq924862077/article/details/48068645

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!