1.1 Object类源码阅读笔记(java8)

作者: 先生zeng | 来源:发表于2019-06-03 15:46 被阅读1次

    在正式开始阅读这个Object类之前,其实之前已经看过源码了,只是一直不知道用什么方式来呈现出我看源码的方式,所以在浏览了不少资料之后,才打算开始写,好了,废话不多说。直接上源码。

    * 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类是所有类的最上层(root),每一个类的最顶层的类都是Object这个类,包括数组。

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

    然后接着往下看,我们可以看到,这个方法使用了native,代表这是一个自然方法,也就是jvm中本地的方法,我们知道java使用c++写的,这里面就是使用了JNI,在java代码里,将外部的c++的方法使用JNI的方式注入到class文件中。在静态代码块中,在编译期的时候就会自动执行了,代表注册本地方法。

    接着往下看,我们可以看到一个同样是本地方法的getClass()


    image.png

    该方法也是一个native方法,同时他是一个静态,表示返回对象的运行时类,返回的对象是由所表示的类的方法所锁定的对象。总结就是返回运行时类名。

    public native int hashCode();
    

    hashCode这个方法,同样也是一个native方法,该方法能返回一个int类型的hash值,通常是将对象内部的地址转换为整数来实现,涉及到对地址分配的操作一般使用c++来操作的,所以也是一个native方法。如果两个对象的equal方法的结果相等,那么两个对象的hashCode的返回值也一定相等。

    public boolean equals(Object obj) {
            return (this == obj);
        }
    

    这个方法是用于判断本对象与传入的另外一个对象是否相等的。该方法声明的结果为true,那么两个对象的hashCode执行的int结果也是相等的。

        /**
         * Creates and returns a copy of this object.  The precise meaning
         * of "copy" may depend on the class of the object. The general
         * intent is that, for any object {@code x}, the expression:
         * <blockquote>
         * <pre>
         * x.clone() != x</pre></blockquote>
         * will be true, and that the expression:
         * <blockquote>
         * <pre>
         * x.clone().getClass() == x.getClass()</pre></blockquote>
         * will be {@code true}, but these are not absolute requirements.
         * While it is typically the case that:
         * <blockquote>
         * <pre>
         * x.clone().equals(x)</pre></blockquote>
         * will be {@code true}, this is not an absolute requirement.
         * <p>
         * By convention, the returned object should be obtained by calling
         * {@code super.clone}.  If a class and all of its superclasses (except
         * {@code Object}) obey this convention, it will be the case that
         * {@code x.clone().getClass() == x.getClass()}.
         * <p>
         * By convention, the object returned by this method should be independent
         * of this object (which is being cloned).  To achieve this independence,
         * it may be necessary to modify one or more fields of the object returned
         * by {@code super.clone} before returning it.  Typically, this means
         * copying any mutable objects that comprise the internal "deep structure"
         * of the object being cloned and replacing the references to these
         * objects with references to the copies.  If a class contains only
         * primitive fields or references to immutable objects, then it is usually
         * the case that no fields in the object returned by {@code super.clone}
         * need to be modified.
         * <p>
         * The method {@code clone} for class {@code Object} performs a
         * specific cloning operation. First, if the class of this object does
         * not implement the interface {@code Cloneable}, then a
         * {@code CloneNotSupportedException} is thrown. Note that all arrays
         * are considered to implement the interface {@code Cloneable} and that
         * the return type of the {@code clone} method of an array type {@code T[]}
         * is {@code T[]} where T is any reference or primitive type.
         * Otherwise, this method creates a new instance of the class of this
         * object and initializes all its fields with exactly the contents of
         * the corresponding fields of this object, as if by assignment; the
         * contents of the fields are not themselves cloned. Thus, this method
         * performs a "shallow copy" of this object, not a "deep copy" operation.
         * <p>
         * The class {@code Object} does not itself implement the interface
         * {@code Cloneable}, so calling the {@code clone} method on an object
         * whose class is {@code Object} will result in throwing an
         * exception at run time.
         *
         * @return     a clone of this instance.
         * @throws  CloneNotSupportedException  if the object's class does not
         *               support the {@code Cloneable} interface. Subclasses
         *               that override the {@code clone} method can also
         *               throw this exception to indicate that an instance cannot
         *               be cloned.
         * @see java.lang.Cloneable
         */
    protected native Object clone() throws CloneNotSupportedException;
    

    首先,我们可以看到,这个方法是一个protected的范围,说明只有继承该类的或者说该类的子类才可以调用。从注解来看,这是一个克隆方法,并且是“浅复制”的。按照惯例,一般是要该方法来克隆一个相同的属性的对象。
    并且有以下的几种情况: 如果

    1. x.clone() != x 结果为true,那么表示x.clone().getClass() == x.getClass()为true
    2. x.clone().equals(x) 为true,这并不是绝对的。

    如果对象的类不支持{@code Cloneable}接口。覆盖{@code clone}方法的子类也可以抛出此异常以指示无法克隆实例。并且需要注意,所有的1数组都有实现{@code Cloneable}接口。

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

    返回对象的字符串表示形式。通常,* {@code toString}方法返回一个“文本表示”此对象的字符串。结果应该是一个简洁但信息丰富的表示,便于人阅读。 建议所有子类都覆盖此方法。该字符串由对象为实例的类的名称组成。at符号字符为`{@code @} ',对象的哈希码的无符号十六进制表示。换句话说,此方法返回一个等于*值的字符串。
    toString()是由对象的类型和其哈希码唯一确定,同一类型但不相等的两个对象分别调用toString()方法返回的结果可能相同。

    public final native void notify();
    

    对java多线程有一定使用的,都知道这是唤醒正在等待此对象的单个线程。如果任何线程正在等待此对象,则选择其中一个被唤醒。与之相对应的是:

    public final native void notifyAll();
    

    他会唤醒所有等待此对象的所有等待线程。

    有唤醒就会有等待状态:

    public final native void wait(long timeout) throws InterruptedException;
    public final void wait(long timeout, int nanos) throws InterruptedException {
    public final void wait() throws InterruptedException {
            wait(0);
        }
    

    根据注释,可知,调用这个方法,会导致当前对象等待,知道另外一个线程为此对象调用notify或者notifyAll,或者经过了指定的时间,当前线程必须拥有此对象的监视器。

    protected void finalize() throws Throwable { }
    

    当垃圾收集*确定没有对该对象的更多引用时,由对象上的垃圾收集器调用。 子类重写{@code finalize}方法以处置系统资源或执行其他清理。
    这个方法用于当对象被回收时调用,默认该类是不做任何调用,这个是对象逃离被JVM干掉的最后的机会,但是任何对象的这个方法只会被调用一次,在《深入理解JVM》一书中提出该函数的调用不确定性大、代价大不建议使用。

    感谢您阅读我的文章,如果满意可以帮我点歌赞,谢谢哈。
    如果对文章部分还有什么见解或者疑惑,可以私信评论我,欢迎技术讨论。如果需要获取完整的文件资源,可以加我微信z985085305,获取我整理的全套笔记。
    思想的碰撞最能促进技术的进步哦。

    相关文章

      网友评论

        本文标题:1.1 Object类源码阅读笔记(java8)

        本文链接:https://www.haomeiwen.com/subject/qdscxctx.html