Object类源码

源码:

package java.lang;

/**
 * **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**
 * 文档大概意思该类是所有类的基类,包括数组也实现了该类的方法
 */
public class Object {
   //一个本地方法,具体是用C(C++)在DLL中实现的,然后通过JNI调用
    private static native void registerNatives();
    static {
    //对象初始化时自动调用此方法
        registerNatives();
    }


    /**
     * Returns the runtime class of this {@code Object}. 
     //返回运行时类
     */
    public final native Class<?> getClass();


   /**
     * Returns a hash code value for the object. 
     //大概的意思就是返回int hashCode值
     */
    public native int hashCode();


    /**
     * Indicates whether some other object is "equal to" this one.
     //比较两个对象内容是否相等
     */
    public boolean equals(Object obj) {
        return (this == obj);
    }


    /**
     * Creates and returns a copy of this object.  The precise meaning
     * of "copy" may depend on the class of the object. 
     * 返回一个复制的对象
     */
    protected native Object clone() throws CloneNotSupportedException;


    /**
     * Returns a string representation of the object. 
     * 返回该对象的字符串表示
     */
    public String toString() {

      return getClass().getName() + "@" +   Integer.toHexString(hashCode());
    }


    /**
     * Wakes up a single thread that is waiting on this object's
     * 唤醒单个线程
     */
    public final native void notify();


    /**
     * Wakes up all threads that are waiting on this object's monitor. 
     * 唤醒所有线程
     */
    public final native void notifyAll();

    /**
     * Causes the current thread to wait until either another thread invokes the
     * {@link java.lang.Object#notify()} method or the
     * {@link java.lang.Object#notifyAll()} method for this object, or a
     * specified amount of time has elapsed
     */
    public final native void wait(long timeout) throws InterruptedException;


    /**
     * Causes the current thread to wait until another thread invokes the
     * {@link java.lang.Object#notify()} method or the
     * {@link java.lang.Object#notifyAll()} method for this object, or
     * some other thread interrupts the current thread, or a certain
     * amount of real time has elapsed.
     * 在其他线程调用此对象的 notify() 方法或 notifyAll() 方法,
     * 或者其他某个线程中断当前线程,或者已超过某个实际时间量前,导致当前线程等待
     */
    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);
    }


    /**
     * Causes the current thread to wait until another thread invokes the
     * {@link java.lang.Object#notify()} method or the
     * {@link java.lang.Object#notifyAll()} method for this object.
     * In other words, this method behaves exactly as if it simply
     * performs the call {@code wait(0)}
     */
    public final void wait() throws InterruptedException {
        wait(0);
    }

    /**
     * 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 {@code finalize} method to dispose of
     * system resources or to perform other cleanup.
     * 当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法
     */
    protected void finalize() throws Throwable { }
}

native关键字标识的java方法为本地方法,底层是有c/c++编写的程序编译后dll文件,java加载dll文件后,可用通过本地方法调用dll中函数。



参考:http://www.cnblogs.com/cq-home/p/6431426.html

          https://blog.csdn.net/jerry_1911/article/details/50962083

猜你喜欢

转载自blog.csdn.net/oqkdws/article/details/79981930